Disabled queries (enabled: false) get affected by invalidateQueries (refetchInactive: true) #3202
Send to my agentDescribe the bug#
queryClient.invalidateQueries(queryKey, { refetchInactive: true, })
also refetches disabled queries (enabled: false)
but - https://github.com/tannerlinsley/react-query/issues/947#issuecomment-1005020047
looks like this issue was introduce in this commit - https://github.com/tannerlinsley/react-query/pull/949/commits/f5ae636e2ba697d56e1789d731e5d382f7b444bd
Your minimal, reproducible example#
https://codesandbox.io/s/quiet-dust-hegny?file=/src/App.js
Steps to reproduce#
- Create a query with
useQueryand setenabledoption to false. - Invalidate this query with
invalidateQueriesand setrefetchInactiveto true
Expected behavior#
I expected that disabled queries (enabled: false) would not refetch when I use invalidateQueries
Platform#
- OS: macOs
- Browser: Not related
react-query version#
v3.34.8
TypeScript version#
v4.2.2
adding { exact : true } prevents refetching. queryClient.invalidateQueries(["key2"], { refetchInactive: true, exact: true });
adding { exact : true } prevents refetching.
no it doesn't, and it shouldn't. With your keys, there should be no difference between exact or fuzzy matching.
@TkDodo why do i get a throttled response with { exact: true }, but get successful response without it?
the api your sandbox queries against has rate limiting. Just use
Promise.resolveinstead!@TkDodo do you have workaround on your mind for now?
+1 here. I've lost about 3 hours investigating why the query having {enabled: false} does fetch. The problem was invalidateQueries((q)=> ...).
@TkDodo do you have workaround on your mind for now?
use invalidateQueries without refetchInactive. Not sure in what case you would need inactive queries (queries that are not mounted) to refetch?
@TkDodo I found workaround for myself, but I hope this issue will be fixed in new version.
TkDodo added labelsbug
TkDodo added labelsv4
:tada: This issue has been resolved in version 4.0.0-alpha.9 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
tannerlinsley added labelsreleased on @alpha
TkDodo closed this
:tada: This issue has been resolved in version 4.0.0-beta.1 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
tannerlinsley added labelsreleased on @beta
Sign in to join this conversation.
Thanks for filing the issue. It seems that a query is considered active` if it has at least one observer that is not disabled. From that definition, it follows that a a query is inactive if it has: a) no observers or b) only disabled observers.
In your case, b) would apply, which is why it is refetched. I've traced it back and it works the same as in v 3.2.0 (the first v3 release), so this is not a regression introduced recently.
However, I think this is wrong and it contradicts the docs where it says:
From what I am seeing, the only check we should be doing is check a) it has no observers. Especially because
enabled: falseis often used when a query is not ready to be fired, like when a dependency is missing (dependent queries).To be on the safe side, I would still suggest to fix this with v4, where we have also changed the queryFilters.