Unexpected query call in suspense mode
See original GitHub issueDescribe the bug
Why is fetchMockApi1 called twice?
const fetchMockApi1 = async () => {
console.log("fetchMockApi1");
return 1;
};
const fetchMockApi2 = async () => {
console.log("fetchMockApi2");
return new Promise((y) => {
setTimeout(() => {
console.log("fetchMockApi2 --- end");
y(1);
}, 1000 * 3);
});
};
function Example() {
const d1 = useQuery(["d1"], fetchMockApi1);
const d2 = useQuery(["d2"], fetchMockApi2);
return <div>test</div>;
}

Your minimal, reproducible example
https://codesandbox.io/s/competent-dew-fomvdq?file=/src/index.js
Steps to reproduce
- go to https://codesandbox.io/s/competent-dew-fomvdq?file=/src/index.js
- click on refresh
- Observe console outputs
Expected behavior
fetchMockApi1 should only be called once
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
MacOS version:4
react-query version
4.0.10
TypeScript version
No response
Additional context
No response
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
queryFn is called twice in Suspense mode · Issue #324 - GitHub
Per the title, when using useQuery with { suspense: true } , the supplied query function will be called twice.
Read more >Suspense | TanStack Query Docs
When using suspense mode, status states and error objects are not needed and are then replaced by usage of the React.
Read more >React Suspense in Practice | CSS-Tricks
This post is about understanding how Suspense works, what it does, and seeing how it can integrate into a real web app. We'll...
Read more >Seeding the Query Cache | TkDodo's blog
The best way to circumvent this problem is to make sure that there is already data in the cache when the component tries...
Read more >Using Suspense and React Query: Tutorial with examples
In this case, React Query fetches data, and Suspense keeps us updated with the app status, as instructed in the App component.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Because of suspense: true suspense unmounts the component and mounts the fallback of the suspense boundary instead. Then, when the request finishes, it will mount the component again. Multiple useQuery calls + suspense create a waterfall, so what happens is:
@dante01yoon can you show what you mean in a codesandbox reproduction?