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

Open
aldenquimby opened this issue Dec 2, 2024 · 0 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 });
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

No branches or pull requests

1 participant