Skip to content
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

useQuery initialData + staleTime should be respected if sibling component uses same queryKey and enabled false #8386

Closed
aldenquimby opened this issue Dec 2, 2024 · 2 comments

Comments

@aldenquimby
Copy link

aldenquimby commented Dec 2, 2024

Describe the bug

When useQuery is called multiple times with the same queryKey, the behavior of enabled, initialData, and staleTime seem off.

useQuery({ queryKey, queryFn, enabled: false });
useQuery({ queryKey, queryFn, initialData: {}, staleTime: Infinity });

I expect the above code to not trigger queryFn because the first call is disabled, and the second has infinitely cached initialData. But queryFn ends up actually getting triggered

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/sparkling-lake-4zgjk5

Steps to reproduce

See codesandbox link

Expected behavior

I expect query is running! to not print to console, but it does print to console

How often does this bug happen?

Always

Screenshots or Videos

No response

Platform

  • OS: Mac Sonoma 14.7
  • Browser: Chrome 131.0.6778.86

Tanstack Query adapter

react-query

TanStack Query version

v5.62.0

TypeScript version

No response

Additional context

If the calls happen in reverse order, then everything works as I'd expect. This does not trigger queryFn:

useQuery({ queryKey, queryFn, initialData: {}, staleTime: Infinity });
useQuery({ queryKey, queryFn, enabled: false });
@TkDodo
Copy link
Collaborator

TkDodo commented Dec 31, 2024

initialData runs when the query is created in the query cache. In the first order of calls, the query is created without initial data. When the second observer then gets added, it picks up the already existing cache entry, which doesn’t have any data. That’s why initialData “doesn’t do anything” here anymore, and the query runs because it has no data yet.

In the second order, initialData is written to the cache (because it’s passed on the query creation), so staleTime will see this as valid data and not trigger a fetch.

All in all, I’d say the current behaviour is expected.

@TkDodo TkDodo closed this as not planned Won't fix, can't repro, duplicate, stale Dec 31, 2024
@aldenquimby
Copy link
Author

Ok, thanks @TkDodo

I cannot easily reorder the existing calls. So my workaround is to add a 3rd useless useQuery to hydrate the cache with initialData before the existing calls. But that is awkward.

Is there anyway to not create that first cache entry? I can't conditionally call a hook, and it sounds like you're saying enabled:false is expected to create an entry.

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

No branches or pull requests

2 participants