T
TanStack•3y ago
rival-black

Invalidate all queries when a certain query runs?

Is there a patter for invalidating all queries when a query with a certain key changes?
4 Replies
vicious-gold
vicious-gold•3y ago
Hi 👋 You can invalidate queries imperatively through the query cache (see: https://tanstack.com/query/v4/docs/react/guides/query-invalidation). What problem are you trying to solve here? A new query key is a new query which won't initially have a cache entry
rival-black
rival-blackOP•3y ago
Late response. Basically we have users that can be assigned multiple accounts to manage. When a user switches their account I want to invalidate all queries so they don't see cached data from the previous account
correct-apricot
correct-apricot•3y ago
Hi. This looks like you should add the account Id in the queryKey. This way you have the isolation of data without the need to invalidate. And depending on cache time / stale time, if a user switches back to an account, you would benefit from the cached data.
vicious-gold
vicious-gold•3y ago
Yeah, your keys should ideally include something about the user that uniquely identifies them. Failing that, you can clear the cache or imperatively clear/refetch/reset queries imperatively via the query cache

Did you find this page helpful?