useQuery randomly not returning data and stuck in loading
See original GitHub issueDescribe 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:
- Created 3 years ago
- Reactions:5
- Comments:61
Top Related StackOverflow Question
@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,
useQuerythinks that it’s still loading the data and never provides it to the app.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