T
TanStack3y ago
correct-apricot

Query invalidation concept question

Hello, if we want to invalidate multiple queries with different query keys, do we need to call invalidateQueries() multiple times per query to invalidate? In our case we are using an array as query keys in the following format:
['resource', args]
['resource', args]
Where args is an object. We sometimes want to invalidate multiple resources and thought this would work:
queryClient.invalidateQueries([
'resource_a',
'resource_b',
]);
queryClient.invalidateQueries([
'resource_a',
'resource_b',
]);
But it doesnt seem to be working.
2 Replies
harsh-harlequin
harsh-harlequin3y ago
Hi. Query Key management is very important when using RQ. With for example a hierarchical structure of key, you can have different invalidation strategies. Must read about that : https://tkdodo.eu/blog/effective-react-query-keys
Effective React Query Keys
Learn how to structure React Query Keys effectively as your App grows
correct-apricot
correct-apricotOP3y ago
Thanks for sharing, I was more inquiring about the specific usage of invalidate query, but after testing it out, yes, if one has different "root" keys then one must call invalidate query multiple times, for each resource to invalidate. My resource queries aren't necessarily tied to each other to warrant adding granularity to the queryKey but they need to be invalidated simultaneously so im simply calling the function multiple times.

Did you find this page helpful?