T
TanStack3y ago
eastern-cyan

Can I call cancelQueries from any queryClient instance?

I don't know how Tanstack Query works deep-down and I have a lot of custom hooks to handle queries. The design of the application I am working on have minimal buttons for a lot of different behavior accross the app and I was wondering If I have to use the queryClient instance from a custom hook to cancel a query or can I create an instance when I need to and passing the appropriate key to cancel my query?
5 Replies
eastern-cyan
eastern-cyan3y ago
The query client instance should be the same as the one passed to the QueryClientProvider
eastern-cyan
eastern-cyanOP3y ago
In a lot of custom hooks (useCustomQuery), I'm instancing a new queryClient to call invalidateQueries for example after a successful mutation. Is it bad? I should keep the queryClient instance in a global state (I use zustand) and then call it from my hook to call my methods such as invalideQueries?
eastern-cyan
eastern-cyan3y ago
Does it actually work? I believe the query keys are scoped inside a query client instance, so on a new instance it wouldn't find the query to invalidate?
environmental-rose
environmental-rose3y ago
This can't work. Always grab the queryClient with useQueryClient
eastern-cyan
eastern-cyanOP3y ago
Sorry for the delay, didn't saw the notifications My bad, I was confused. I am using the useQueryClient hook to get the queryClient instance and passed down to the provider everywhere in the project.

Did you find this page helpful?