apollo react查询后调度redux操作
我想在查询完成后立即发送redux操作在哪里做这件事最合适 在这里,我保留了对apollo react查询后调度redux操作,redux,react-apollo,Redux,React Apollo,我想在查询完成后立即发送redux操作在哪里做这件事最合适 在这里,我保留了对refetch函数的引用,以便在以后使用最新数据轻松更新视图 export default graphql( allFilesQuery, { props: ({ ownProps, data }) => { const { dispatch } = ownProps; dispatch( setRef
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`.
props函数应该是纯函数,返回props以注入组件,而不会产生任何副作用。实际上,通过将分派包装在
setTimeout
中,您可能可以在其中分派,但这是一个非常糟糕的主意,因为props
函数在每次组件重新渲染时都会运行,可能会触发许多不需要的分派。如果分派使组件重新渲染,甚至可能导致无限循环
执行所需操作的正确位置在组件中。您可以使用组件将接收道具
(或其他生命周期),并将以前的道具与下一个道具进行比较,在适当时触发分派。您可以使用data.networkStatus
或data.loading