You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
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.
Describe the bug
When
useQuery
is called multiple times with the same queryKey, the behavior of enabled, initialData, and staleTime seem off.I expect the above code to not trigger
queryFn
because the first call is disabled, and the second has infinitely cached initialData. ButqueryFn
ends up actually getting triggeredYour 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 consoleHow often does this bug happen?
Always
Screenshots or Videos
No response
Platform
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
:The text was updated successfully, but these errors were encountered: