Skip to content
Source
Sign inSign up
TanStack/query

Disabled queries (enabled: false) get affected by invalidateQueries (refetchInactive: true) #3202

Send to my agent
Closednikkizol(GitHub profile, opens in a new tab) opened this issue 11 comments

Describe 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

  1. Create a query with useQuery and set enabled option to false.
  2. Invalidate this query with invalidateQueries and set refetchInactive to 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

  1. 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:

    When enabled is false: The query will ignore query client invalidateQueries and refetchQueries calls that would normally result in the query refetching.

    From what I am seeing, the only check we should be doing is check a) it has no observers. Especially because enabled: false is 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.

  2. adding { exact : true } prevents refetching. queryClient.invalidateQueries(["key2"], { refetchInactive: true, exact: true });

  3. 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.

  4. @TkDodo why do i get a throttled response with { exact: true }, but get successful response without it?

  5. the api your sandbox queries against has rate limiting. Just use Promise.resolve instead!

  6. @TkDodo do you have workaround on your mind for now?

  7. +1 here. I've lost about 3 hours investigating why the query having {enabled: false} does fetch. The problem was invalidateQueries((q)=> ...).

  8. @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?

  9. @TkDodo I found workaround for myself, but I hope this issue will be fixed in new version.

  10. TkDodo added labelsbug

  11. TkDodo added labelsv4

  12. :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:

  13. tannerlinsley added labelsreleased on @alpha

  14. TkDodo closed this

  15. :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:

  16. tannerlinsley added labelsreleased on @beta

Sign in to join this conversation.