Describe the bug#
Some components using the same useQuery hook get consistently stuck in (fetching/loading) state when they should be rendering a result (idle/success).#
The example here is a bit contrived to get a minimal repro, but this is a legitimate usecase/ bug.
Your minimal, reproducible example#
https://codesandbox.io/p/devbox/weathered-framework-ktk4kh?file=%2Fsrc%2Findex.jsx%3A40%2C21
Steps to reproduce#
Apparent pre-requisites from the narrowing I was able to do:#
- Non-zero
staleTime
- Changes to
enabled + queryKey values coinciding with a state change that happens after the initial render.
- Either via a microtask:
Promise.resolve()/queueMicrotask
- Or macrotask:
setTimeout.
- Several react components must render (consuming the same data)
Also interesting to note: the "last" component to render gets stuck when queueing the state change a microtask and the "second" one always get's stuck when queuing the change as a new macrotask. (Obviously which element gets stuck is likely some strange cross interaction with React's internal queuing of component updates/commits).
Expected behavior#
I expect all rendered components to enter the "loaded" state.
How often does this bug happen?#
Every time. (can be forced into a race condition by lowering staleTime)
Screenshots or Videos#
https://github.com/TanStack/query/assets/5288805/b2a57fa2-0cef-4fa8-a6c4-f25b6aa62c5b
Platform#
react-query: ^4.33.0
react: ^16.14.0"
Chrome: 122.0.6261.94 (Official Build) (arm64) (MacOS)
Tanstack Query adapter#
react-query
TanStack Query version#
v4.33.0
TypeScript version#
No response
Additional context#
No response
your sandbox works fine after moving to react18 and TanStack Query v5:
https://codesandbox.io/p/devbox/spring-star-fgmywp
I'm sorry but I don't think I'm going to look into this combination (v4 + React16). You can try if upgrading either solves the problem, but updating both definitely does.