useQuery randomly not returning data and stuck in loading

See original GitHub issue

Describe the bug After upgrading to react-query 3.5.11 from 2.26.4 last week, our project’s Cypress tests started failing randomly because the app started showing a loading spinner at random times. After a lot of head-banging, we finally tracked down the issue: even though, based on the network inspector, the request had gone through successfully, react-query’s useQuery was returning an object with isLoading as true and data as undefined. Reverting back to react-query 2 fixed the problems.

Most of the requests in our Cypress tests are stubbed using cy.server and cy.route. This probably shouldn’t affect react-query but mentioning it here just in case.

Our cache is configured in the following manner at the top of one of our source files, so this shouldn’t be caused by us accidentally creating multiple caches:

const queryCache = new QueryCache({
  defaultConfig: {
    queries: {
      staleTime: 10 * 60 * 1000,
      cacheTime: 15 * 60 * 1000,
      retry: (
        _failureCount,
        error: any,
      ) => error.status !== 403 && error.status !== 401,
    },
  },
});

// Further down:

class AppProvider extends Component<{}, State> {
  public render() {
    return (
      <ReactQueryCacheProvider queryCache={queryCache}>
        {/* ... */}
      </ReactQueryCacheProvider>
    );
  }
}

To Reproduce Unfortunately, this happens randomly and is thus very hard to reproduce. The randomness does point towards some type of race condition. Our app performs many (~10) requests on startup.

Even though which tests fail is random, I can consistently get at least one test to fail. Is there something I can do to help track down what could be the issue?

Expected behavior useQuery should return the data it receives in the response once the request is successful.

Desktop (please complete the following information):

  • OS: MacOS
  • Browser: Chrome
  • Version: 87

Additional context Happens on both Cypress version 5.3.0 and 6.2.1.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:61

github_iconTop GitHub Comments

6reactions
vsaarinencommented, Jan 29, 2021

@fermmm It sounds like your issue may be different than what I’m experiencing. My issue is that even though data was successfully received by the app, useQuery thinks that it’s still loading the data and never provides it to the app.

2reactions
nawfalhaddicommented, Dec 14, 2021

I also have the same issue in my react native application, it is a random issue, sometimes isLoading and isFetching get stuck and data stays undefined even if the network request is done successfully. When I restart the app it works fine! Hopefully they can fix this issue as soon as possible

Read more comments on GitHub >

github_iconTop Results From Across the Web

useQuery randomly not returning data and stuck in loading
Unfortunately, this happens randomly and is thus very hard to reproduce. The randomness does point towards some type of race condition. Our app ......
Read more >
React-query is returning data as undefined and isLoading is ...
on react native in a random scenario react-query get stuck in loading state where isLoading is always true and the data is undefined....
Read more >
Loading stuck on true when using useLazyQuery in React - Help
Hey fellas, i'm using useLazyQuery to login to the server. it was working great until today. It suddenly stopped working.
Read more >
Disabling/Pausing Queries | TanStack Query Docs
Lazy queries will be in status: 'loading' right from the start because loading means that there is no data yet. This is technically...
Read more >
React Query—Fetching Data the Right Way - Telerik
Let's learn how to use React Query, which is a data-fetching library that uses a set of hooks to handle fetching for our...
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