当需求需要走其他路径时如何触发重新获取?
到目前为止,我可以通过儿童组件传递Refetch功能。问题是当我必须转到另一页并使用redux时。 (新路径链接的打开对话框)。在该页面中,我将触发此Refetch功能。虽然redux肯定无法保存功能倒流。解决这个问题的正确方法是什么?
const { data: getWorkerGroupsResponse, refetch } = useQuery(
'get-worker-group',
async () => getWorkerGroups(projectId as string),
{
enabled: !!projectId,
},
);
在上面的代码中,我必须将Refetch传递到其他页面。
So far I can pass refetch function on a child component. The problem is when I have to go to other page and use redux. (Open dialog for new path link). And in that page I will trigger this refetch function. While redux for sure can not save a function refetch. What is the proper way to solve this ?
const { data: getWorkerGroupsResponse, refetch } = useQuery(
'get-worker-group',
async () => getWorkerGroups(projectId as string),
{
enabled: !!projectId,
},
);
In code above I have to pass refetch to other page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当查询键更改时,React查询手柄会自动换取。您不应仅仅因为ID发生了变化,因此不应发现需要调用
Refetch
。您的组件中有一个错误,即查询密钥不包含ID,因此从RQ角度来看,只有一个查询。如果您介绍它,即使不手动进行拆除也可以:
现在,当
ProjectID
更改时,React Query会选择您提供的更改并将重新运行的事实。使用新值查询。React query handles refetches automatically when the query key changes. You should not find the need to call
refetch
just because an ID has changed.You have a bug in your component that the query key does not include the id, so from RQ perspective, there is just one query. If you introduce it, it will be fine even without refetching manually:
Now, when
projectId
changes, react query picks up on the fact that thequeryKey
you provided changes and will rerun the query using the new value.