invalidate query after queryClient.clear() no longer works
I have an app where I clear the query client using
queryClient.clear() when the user changes the app langauge. It makes sense because I want all queries in the previous language to be cleared. Anyways after that I try to invalidate previously mounted queries and they no longer invalidate, however invalidating the queries before clearing inavlidates them correctly.
What am I doing wrong? Why can't I invalidate after clearing?
Here is a code sandbox:
https://codesandbox.io/s/late-river-h8mt4h?file=/src/App.jsitsramiel
CodeSandbox
late-river-h8mt4h - CodeSandbox
late-river-h8mt4h by itsramiel using @tanstack/react-query, react, react-dom, react-scripts
6 Replies
continuing-cyan•3y ago
Hi 👋
I think this might happen because there's no query to invalidate after clearing the cache.
Having said that, if you clear the cache, it re-fetches once but then doesn't re-fetch if you clear it again. This behaviour is consistent with
removeQueries but I'm not entirely sure why clearing the cache doesn't always "reset" active queries, although I imagine it's an intentional decision.
Have you considered using resetQueries or refetchQueries for your use case?
I'm intrigued about the behaviour I described above though. Interesting!blank-aquamarineOP•3y ago
Hmmm, so in my case
refetchQueries makes the most sense I guess?continuing-cyan•3y ago
I think
resetQueries might be slightly more correct here as I'm not sure if there are nuances surrounding refetching inactive queries and whether they'd hold on to their stale data if they're unmounted at the time of refetch.blank-aquamarineOP•3y ago
Alright I appreciate it. Thank you 🙏🏻
continuing-cyan•3y ago
No worries :reactquery: I'd still be interested to find out about the cache clearing behaviour though; I'll let you know 🙂
eager-peach•3y ago
Another approach that could make sense : integrate the current language right in the query key. This way no need bothering clearing stuff. Also if you need to reset queries this means that their data depend on the language and thus it should be part of the key