简体   繁体   English

如果我在 Next.js 路线之间导航,invalidateQueries 将停止工作

[英]invalidateQueries stops working if I navigate between Next.js routes

We used queryClient.invalidateQueries(someQueryKey) to refetch data in queries and its works well.我们使用queryClient.invalidateQueries(someQueryKey)来重新获取查询中的数据,并且效果很好。 Project migrated to Next.js and router changed from react-router to next-router code base stays maximum the same except for router changes.项目迁移到Next.js和路由器从react-router更改为next-router代码库保持最大相同,除了路由器更改。

Now if I navigate from one route to another and then go back, queryClient.invalidateQueries(someQueryKey) stops working, all other query calls work as it should useQueris and useMutations.现在,如果我从一条路线导航到另一条路线,然后返回 go, queryClient.invalidateQueries(someQueryKey)停止工作,所有其他查询调用都可以正常工作,因为它应该使用 Queris 和 useMutations。

What weird, when I navigate between the pages queries disappear from Query Dev Tools but continue to work, except of queryClient.invalidateQueries() .奇怪的是,当我在页面之间导航时,查询从Query Dev Tools中消失但继续工作,除了queryClient.invalidateQueries() If I refresh the page Query Dev Tools starts to show me the correct queries and it starts working correctly until I navigate to another route and back.如果我刷新页面Query Dev Tools开始向我显示正确的查询,并且它开始正常工作,直到我导航到另一条路线并返回。

I'd really appreciate if anyone can help me with this issue.如果有人可以帮助我解决这个问题,我将不胜感激。 Let me know if any additional info is required.让我知道是否需要任何其他信息。

Discussion moved to : https://github.com/tannerlinsley/react-query/discussions/3037讨论移至https://github.com/tannerlinsley/react-query/discussions/3037

answer: The following seems to fix the issue, indeed, the previous one was creating a new client every time, so I assume that is why the query invalidations weren't working properly:答案:以下似乎解决了这个问题,确实,前一个每次都创建一个新客户端,所以我认为这就是查询失效无法正常工作的原因:

export function useReactQueryClient() { 
  const [queryClient] = React.useState(function () {
    return new QueryClient({
      defaultOptions: {
        queries: {
          onError(err) {
            // ....
          },
        },
      },
    });
  });

  return queryClient;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM