query: Infinate querying when query fails and using suspense + enabled flag
Describe the bug Using suspense + enabled flag to delay querying, when a query fails will cause infinite querying.
To Reproduce
Core code:
function query() {
return new Promise((resolve, reject) => {
console.count("query");
setTimeout(() => reject("Query Error"), 1000);
});
}
function DataFetch() {
const [enabled, setEnabledStatus] = useState(false);
useQuery("mock", query, { suspense: true, enabled, retry: 0 });
return <button onClick={() => setEnabledStatus(true)}>Start</button>;
}
Complete code: https://codesandbox.io/s/react-query-bug-suspense-enabled-flag-2w762?file=/src/App.tsx
Steps to reproduce the behavior:
- Click the
StartButton - Look at
Console, you will see many queries.
Expected behavior Throw Error and only query once.
Screenshots

Desktop (please complete the following information):
- OS: macOS
- Browser: chrome
- Version: 91.0.4472.114
Additional context
After removing the enabled or suspense flag, anything works well.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 8
- Comments: 21 (4 by maintainers)
Commits related to this issue
- fix(core): remove enabled condition in queryObserver https://github.com/tannerlinsley/react-query/issues/2434 — committed to JaeYeopHan/react-query by JaeYeopHan 3 years ago
- fix(core): remove enabled condition in queryObserver for suspense (#3093) * fix(core): remove enabled condition in queryObserver https://github.com/tannerlinsley/react-query/issues/2434 * chore... — committed to TanStack/query by JaeYeopHan 3 years ago
- fix(core): remove enabled condition in queryObserver for suspense (#3093) * fix(core): remove enabled condition in queryObserver https://github.com/tannerlinsley/react-query/issues/2434 * chore... — committed to Olaf0703/query by Olaf0703 3 years ago
- fix(core): remove enabled condition in queryObserver for suspense (#3093) * fix(core): remove enabled condition in queryObserver https://github.com/tannerlinsley/react-query/issues/2434 * chore... — committed to MarttiCheng/TanStack-Query by MarttiCheng 3 years ago
- fix(core): remove enabled condition in queryObserver for suspense (#3093) * fix(core): remove enabled condition in queryObserver https://github.com/tannerlinsley/react-query/issues/2434 * chore... — committed to SerhiiMisiura/TanStack-Query by SerhiiMisiura 3 years ago
π This issue has been resolved in version 3.34.3 π
The release is available on:
Your semantic-release bot π¦π
Popping in to say thank you guys so much for resolving this! The fix came just as we had identified this as a prod issue and really saved me a ton of grief : )
yeah, could be. I would trust the tests here. If the new test works and the old test still works, too, Iβm happy to merge this π
sure, I think we added a test case for the other situation as well so that we donβt break it again. please add the test from this issue as well so that we can see that it passes.
also tagging @JaeYeopHan - maybe he remembers why this condition was added?