Skip to content

invalidateQueries doesn't work when enabled attribute is set to false #947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sayinserdar opened this issue Sep 1, 2020 · 12 comments · Fixed by #949
Closed

invalidateQueries doesn't work when enabled attribute is set to false #947

sayinserdar opened this issue Sep 1, 2020 · 12 comments · Fixed by #949

Comments

@sayinserdar
Copy link

queryCache.invalidateQueries() function doesn't work if the related useQuery definition has { enabled: false } configuration.

Here is the definition of my 'menus'
Screen Shot 2020-09-01 at 12 49 46 PM

If I try to create a new 'menu', invalidateQueries works as expected
Screen Shot 2020-09-01 at 12 51 13 PM

But if I add a configuration as { enabled: false} as in the below image invalidateQueries function doesn't work
Screen Shot 2020-09-01 at 12 50 01 PM

I've spent too many hours on this issue. I changed every other details/configuration but nothing helped. I also tried my other queryKeys other than 'menus' but the problem remains. In my case, it's not a big deal to remove the { enable: false } configuration but is this the expected output ?

  • OS: macOS
  • Browser: Any browser
  • 10.15.3

React Query - v2.12.1
React Query Developer Tools - v2.3.2

Apart from this issue React Query has been smooth and wonderful. Thanks everyone who contributes this project.
Peace

@boschni
Copy link
Collaborator

boschni commented Sep 1, 2020

What happens if you set refetchInactive to true? https://react-query.tanstack.com/docs/api#querycacheinvalidatequeries

@sayinserdar
Copy link
Author

What happens if you set refetchInactive to true? https://react-query.tanstack.com/docs/api#querycacheinvalidatequeries

Unfortunately, nothing has changed. Doesn't invalidate

@jsangaritar
Copy link

Hi, im using "react-query": "^3.24.3" and this issue is still happening, i have a query defined with enable: false and when i want to invalidate it does not work with that config.

@TkDodo
Copy link
Collaborator

TkDodo commented Jan 4, 2022

disabled and inactive are not the same thing. Disabled queries will never be refetched, except with refetch() returned from useQuery. Documented here: https://react-query.tanstack.com/guides/disabling-queries

@jsangaritar
Copy link

jsangaritar commented Jan 10, 2022 via email

@TkDodo
Copy link
Collaborator

TkDodo commented Jan 10, 2022

@jsangaritar I'm still wondering what your use-case is where you disable a query but then want to trigger a refetch of that disabled query from another component... it seems like this doesn't work with RQ, but a bit against it. The query will still be disabled after refetch and you won't benefit from smart revalidations ...

@Maximilon
Copy link

Maximilon commented Aug 9, 2022

@jsangaritar I'm still wondering what your use-case is where you disable a query but then want to trigger a refetch of that disabled query from another component... it seems like this doesn't work with RQ, but a bit against it. The query will still be disabled after refetch and you won't benefit from smart re-validations ...

Hi TkDodo

I have a use case where I am wanting to load queries and receive items based on a button click and then delete one of those items on the UI.

I have set up the enabled:false so that the query retrieves the list of items based on the button press.

User actions:
user presses the button, useQuery returns data
data is mapped and also populated with a delete button next to each item
the delete button is pressed triggering a mutation
mutation succeeds and invalidation runs

Current behavior
The mutation succeeds, but the page does update the UI

Discussion
Invalidation works brilliantly without the enabled:false flag -- if I omit the flag then I get the real-time refresh behaviour (based on invalidate) however, I want the enabled flag so that the query only runs on the original button press/submit

So Indeed my understanding this is a bug or a feature that seems counter intuitive as it works without the flag just fine.

Workaround
I replaced invalidateQueries with refetchQuery option onSuccess callback and it provides expected user behaviour, but is this the most suitable pattern?

If that is not how I should go about this please advise. But in short, this pattern seems natural, my code is almost identical to the OPs original example.

@TkDodo
Copy link
Collaborator

TkDodo commented Aug 13, 2022

once you set enabled: false, you opt out of many things. enabled: false basically means that the query is not ready to run. Lost of people use it conditionally, like: enabled: !!id to not fetch while the id is not there yet. It will automatically enable itself once id is ready.

Hence it is very much on purpose that invalidation skips disabled queries and this will not change.

For your case, you might also want to not set enabled to hardcoded false, but to a state value that changes from false to true once you click that button. See also lazy queries

@pdroid
Copy link

pdroid commented Aug 31, 2022

Hi! Contributing with our use case as well:

A query is enabled if the user has logged in, and disabled if logged out (i.e. enabled: !!user).

As there is no option to invalidate the data once it is disabled, the only sane approach was to move the enable-logic to the fetch method, returning the equivalent of invalidated data if the user is logged out, thus replacing the data.

This isn't a big headache, but it could also be avoided with an option invalidateOnPause.

@imagine10255
Copy link

imagine10255 commented Apr 8, 2023

In the case of query is enabled: false, but the user needs to log out and clear him, you can use this method

const qc = useQueryClient();
qc.resetQueries([ECacheKey.PROFILE_ME]);

Hope it helps someone who has the same problem as me

"@tanstack/react-query": "^4.28.0",

@yourcasualdev
Copy link

I don't think if there is anyone dumb like me encounters this mistake but I leave it for who does.

Apparently wagmi client has the same method useQueryClient and if you use something like VSCode auto import with pushing tab, you can mistakenly import Wagmi like

import {  useQueryClient  } from 'wagmi';

instead of

import {  useQueryClient  } from 'react-query';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants