-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Description
Describe the bug
When using useQuery
with suspense: true
(or useSuspenseQuery
) in combination with cacheTime: 0
and a synchronous query function, the component enters an infinite re-render loop.
useQuery(
['key'],
() => 42, // synchronous query function that returns immediately
{ suspense: true, cacheTime: 0 }
);
This happens in @tanstack/react-query
version 4.24.2
or higher, but below 5.51.23
. More specifically, this issue is introduced by this commit and fixed by this commit.
While this issue has been resolved in recent v5 versions, it remains unfixed in recent v4 versions. Since our team is currently using 4.x.x
, I would appreciate guidance on whether it would be appropriate to submit a PR that backports a fix like this PR to the 4.x.x
branch.
Your minimal, reproducible example
https://stackblitz.com/edit/vitejs-vite-xrytufnd?file=src%2FApp.tsx
Steps to reproduce
- Open the StackBlitz MCVE.
- Navigate to App.tsx.
- Observe that the suspended App component never resolves.
- Check the browser DevTools console - you'll see the "Render App" message being printed infinitely, indicating that the App component is stuck in an infinite re-render loop.
Expected behavior
The useQuery
hook should not trigger infinite re-renders and the suspense boundary should resolve normally.
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
- OS: macOS
- Browser: Chrome
Tanstack Query adapter
react-query
TanStack Query version
v4.24.2 or higher, but below v5.51.23
TypeScript version
No response
Additional context
No response