question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected query call in suspense mode

See original GitHub issue

Describe 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>;
}

image

Your minimal, reproducible example

https://codesandbox.io/s/competent-dew-fomvdq?file=/src/index.js

Steps to reproduce

  1. go to https://codesandbox.io/s/competent-dew-fomvdq?file=/src/index.js
  2. click on refresh
  3. 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:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
TkDodocommented, Jul 24, 2022

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:

  • component mounts, d1 fetches, component suspends
  • d1 finishes, component re-mounts, d2 fetches, component suspends
  • d2 finishes, component re-mounts. At this time, data for d1 is considered stale ( i staleTime is set), so you’ll see a background refetch because of refetchOnMount
0reactions
TkDodocommented, Nov 11, 2022

@dante01yoon can you show what you mean in a codesandbox reproduction?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found