我已经使用Apollo创建了一个React组件,目前我使用render函数在数据加载后执行重定向,但是我收到警告:Warning: Cannot update during an existing state transition (such as within
render ). Render methods should be a pure function of props and state.
这是我的代码: https://gist.github.com/jmn/c3f7c4489cd5f759808a62b48352ce87
重定向部分:
render() {
return (
<Query query={Q} variables={{ cursor: this.props.match.params.id }}>
{({ loading, error, data, fetchMore }) => {
if (loading) return <p>Loading...</p>;
if (error) return <p>Error :(</p>;
if (!this.props.match.params.id) {
this.props.history.push(
"/post/" + data.allPosts.pageInfo.endCursor
);
}
return (
<div></div>
)
如何在惯用的React代码中执行此操作并避免出现任何警告?