当需求需要走其他路径时如何触发重新获取?

发布于 2025-01-19 12:41:58 字数 369 浏览 1 评论 0原文

到目前为止,我可以通过儿童组件传递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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的巷 2025-01-26 12:41:58

当查询键更改时,React查询手柄会自动换取。您不应仅仅因为ID发生了变化,因此不应发现需要调用Refetch

您的组件中有一个错误,即查询密钥不包含ID,因此从RQ角度来看,只有一个查询。如果您介绍它,即使不手动进行拆除也可以:

const { data: getWorkerGroupsResponse, refetch } = useQuery(
  ['get-worker-group', projectId], // projectId should be part of queryKey
  async () => getWorkerGroups(projectId as string),
  {
    enabled: !!projectId,
  },
);

现在,当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:

const { data: getWorkerGroupsResponse, refetch } = useQuery(
  ['get-worker-group', projectId], // projectId should be part of queryKey
  async () => getWorkerGroups(projectId as string),
  {
    enabled: !!projectId,
  },
);

Now, when projectId changes, react query picks up on the fact that the queryKey you provided changes and will rerun the query using the new value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文