我想在查询完成后立即发送一个redux操作。-哪里是做这件事的合适地点?
在这里,我保留了对refetch函数的引用,这样我就可以在稍后的时间用最新的数据轻松地更新视图。
export default graphql(
allFilesQuery,
{
props: ({ ownProps, data }) => {
const { dispatch } = ownProps;
dispatch(
setRefetchAllFiles(data.refetch)
);
return {
data,
...ownProps,
};
}
}
)(FileListComponent);在这起作用的同时,我也收到一个警告,说:
Warning: setState(...): Cannot update during an existing state transition (such as within `render` or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to `componentWillMount`.发布于 2017-09-08 22:01:46
道具功能应该是纯的,并返回道具注入组件,而不执行任何形式的副作用。实际上,您可以在其中进行分派,方法是将分派包装在setTimeout中,但这将是一个非常糟糕的主意,因为props函数每次组件重新呈现时都会运行,并且可能会触发许多不必要的分发。如果您的分派使组件重新呈现,它甚至可能导致无限循环。
做您想做的事情的正确位置是在组件中。您可以使用componentWillReceiveProps (或其他生命周期),并在适当时将以前的道具与下一个道具进行比较,触发分派。为此,您可以使用data.networkStatus或data.loading。
https://stackoverflow.com/questions/46094183
复制相似问题