【发布时间】:2019-05-22 07:01:02
【问题描述】:
我使用 Apollo 创建了一个 React 组件,我目前使用渲染函数在数据加载后执行重定向,但我收到警告:Warning: Cannot update during an existing state transition (such as withinrender). 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 代码中做到这一点并避免任何警告?
【问题讨论】:
标签: reactjs react-router apollo