T
TanStack2y ago
equal-aqua

How to access queryClient to invalidate cache?

In my Next app, I am initializing queryClient from _app.tsx. I have some instances where I want to invalidate cache (for example when I mutate data). From the docs (v5) it seems that the only way to do this is through queryClient. What do you think of something like this, is it okay? It bugs me that I am importing something from _app.tsx, at the same time I am not sure how to do otherwise.
export const queryClient = new QueryClient();

function App({ Component, pageProps }: AppProps) {
return (
<QueryClientProvider client={queryClient}>
<Layout>
<Component {...pageProps} />
</Layout>
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
);
}

export default App;
export const queryClient = new QueryClient();

function App({ Component, pageProps }: AppProps) {
return (
<QueryClientProvider client={queryClient}>
<Layout>
<Component {...pageProps} />
</Layout>
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
);
}

export default App;
Thank you!
4 Replies
equal-aqua
equal-aquaOP2y ago
I just saw this, it seems to do exactly what I need! https://tanstack.com/query/latest/docs/framework/react/reference/useQueryClient
useQueryClient | TanStack Query Docs
The useQueryClient hook returns the current QueryClient instance. `tsx
equal-aqua
equal-aquaOP2y ago
I am still kind of skeptical though xD
broad-brown
broad-brown2y ago
React Query FAQs
Answering the most frequently asked React Query questions
equal-aqua
equal-aquaOP2y ago
thank you Dominik, again haha

Did you find this page helpful?