Leveraging QueryClient
As you are aware, QueryClient
allows you to interact with your cache.
In the previous chapter, we saw how QueryClient
could be leveraged to trigger refetching a query. What we haven’t seen yet is how QueryClient
can be used for much more things.
To use QueryClient
in your components, you can leverage the useQueryClient
hook to access it. Then, all you have to do is call the method you need.
In this section, we’ll see how you can use QueryClient
to solve more server state challenges such as the following:
- Query invalidation
- Prefetching
- Query cancelation
Before we start query invalidation, one thing to be aware of is that some of these methods, namely the ones we are going to see, can receive certain query filters to help you match with the right queries.
In the previous chapter, we saw the following example for query refetching:
queryClient.refetchQueries({ queryKey: ["api"] })
The preceding snippet...