-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
useQuery randomly not returning data and stuck in loading #1657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
this looks surprisingly similar to #1653 |
Don't know how the tests are written but it might be good to check if they do not implicitly depend on certain timings. Are you able to reproduce it manually? This might be useful: https://docs.cypress.io/guides/core-concepts/retry-ability.html |
I haven't noticed this problem when using the app manually, so there's always a chance that this is somehow caused by Cypress. But a few reasons why I think this may be an issue with react-query:
I'll see if I can narrow down the react-query version where this started happening. |
I can confirm the same behavior after migrating from 2.23.1 to 3.5.16. |
Would be great if you guys could to provide a sandbox or something similar which we can use to debug the issue. |
I've tried to reproduce in a sandbox without any luck ( |
Commenting just to subscribe to the thread because I'm having the same issue and can't reproduce it on a sandbox. Happens on v2.26.3 and after updating to v3.6.0. |
Can we try to narrow it down collectively? So far, we've seen that:
could you further narrow it down to a specific version between those two?
|
I'm also seeing that both the opening post in this issue as well as in #1653 have the |
@TkDodo I set |
For me, things work fine on |
Started seeing the same issues after upgrading to |
I have the same issue |
I finally found the cause at least for my specific case. React-query performs a deep compare to update the component, so if your request returns the same data than in the previous request, react query will not update your component. This was happening randomly to me because the response of my request contains random changes but sometimes returns the same data. In my case I expect a re-render always.
This is a bug or at least confusing because it's an "anti-pattern" since react does not expose anything that works with deep compare like this. React is based on reference equality only. This feature was introduced recently: |
@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, |
Yes my problem looks similar but may be not the same. |
Ok I had this issue in another request of my app and this time was not related with structuralSharing, I noticed {data: undefined, isLoading: true} randomly without getting the data. The only different thing with this useQuery() it was that I had many components mounted using the same useQuery() key, maybe is something that happens when one component is waiting for the request and other mounts with the same... I don't know. |
Same problem here. In my case I use |
I would really like to get to the bottom of this issue, but if nobody can reproduce it in a codesandbox, it is almost impossible to narrow down. |
I forgot to add more info: I'm mounting a lot of components (50) that use this |
I have created a sandbox with the conditions that seem to be common to all of us (Infinity staleTime and having many instances) but cannot reproduce the error, I think it has only happened to me once. On my SPA it happens to me much more easily 🤔 |
From what I can see in the sandbox: are you also putting the whole result from |
No no, it was like this because since I don't use |
In my case this only happens when I restart my RN app when all the components are loading for the first time |
I believe my issue has also appeared only on app start, in the initial loading of components. |
I guess this is the only time when you can be "stuck in loading", because you are never in loading state again once you've successfully loaded data. Question: In case this being-stuck happens, what do the react-query devtools say about the state of the query? |
Yes, that does indeed seem weird. Are you using different |
I've never used
Could the issue be somehow related to the fact that the devtools marks the query as stale even though it shouldn't be? Just guessing here since I don't know what "stale" means... |
Hi @TkDodo, I'm sorry for disturbing you, but could you check if this fix is present in v4, please? Screen.Recording.2023-02-01.at.16.01.17.movI've spent many hours trying to track the root cause o the issue, because it happens only on CI (like 1/5 times) on my project and then found this issue for v3. |
I renamed the port-to-v3 label as port-to-v4, that was probably not a good idea, sorry. code is quite different now, I would need to see a codesandbox reproduction that fails on v4 please. the code path you're showing has been removed in a later v3 version to ensure concurrent rendering capabilities: |
I am going to add a +1 to this. I am facing this exact issue where Here's whats happening
Component 1 and 2 are quite similar in how they work, Query-1 and Query-2 uses the same async functions (with different service endpoints). I will say that at some point I had this
Changing this to the following didn't work either
|
ok sheesh i think i found the issue. With Multiple queries comes multiple |
I am experiencing this issue consistently in a larger project with
but only if react is running in strict mode. Unfortunately I do not have a minimal repro example. |
I am facing the same issue only in Cypress. (Edit: working in Playwright, switching to it) Devtools indicate that the data is fresh but the loader is still loading (isFetching). I am using react query for server state and redux for client state. Could be related |
I'm also facing the same issue since recently on v4. invalidating a query (as result of a mutation, or manually via dev tools) will make it transition to 'isFetching', but won't transition again to 'success'. DevTools show it stuck in fetching. Components using the query will render only once and see it in 'success' status with old data, never receiving the updated data. Unfortunately no minimal repro from me either, but I have noticed that this happened once I moved some of the query dependencies out of a context and inside a state manager (Jotai). In theory this does not imply any change in the query rendering, but the timing is very suspicious in my case |
One thing to remember if you're in the MSW/Testing land - set Remember that Query intentionally backs off over a period of time to retry searching, upwards of a couple of seconds. This is a good thing, as it leads to better UX. As a result, my tests were failing until I did this. |
We also faced the same issue where for some reason some users were not able to fetch the data properly. On further debugging found the fetchStatus was always So this probably means something wrong is happening in the code when the default cc @TkDodo NOTE: Using the latest version current: 4.35.3 |
no, this is an issue with
Since |
Thanks for the quick reply, will upgrade to v5. |
See am encountering the same issue with v4.36, |
We have a large number of users using the app, occasionally a few users encounter this issue where all the functions using react query get stuck at the loading stage, whereas direct calls using axios to the API work fine. The characteristic is that the loading is shown, but no requests seem to be made to the server. When we instruct the users to completely close the browser and reopen it, everything starts working normally again. It's quite strange.
|
again, I'm positive that this is related to you can validate that assumption by looking at the again, you can:
|
Thank you for responding, however, it happens randomly across different browsers including Edge, Brave, etc. |
edge and brave are both built on chromium ... they are the same thing and have the same behaviour / bugs in this regard |
Firefox 120.0.1 (64-bit) still occurring M2 Mac, Sonoma 14.1.1 |
@117 on query v4 or v5? We haven't done anything on v4 - and also won't because the fix / workaround was a change in behaviour, so it's a breaking change |
I've had the same problem for a while and haven't bothered to investigate until now. I've now figured out the reason which for me was that I called Here's a sandbox: https://codesandbox.io/p/sandbox/react-query-bug-sfm3fx?file=%2Fsrc%2FApp.tsx%3A16%2C2 I've also included the fix that worked for me, which you can toggle on/off using the Is this a bug in React Query or just bad implementation on my part? @TkDodo |
@lundstromdavid it seems like you're clearing the cache while the query is in-flight, I don't know why you would do something like that. |
@TkDodo Thanks for the reply! Should have read up more before. Although it does seem quite unintuitive for me that |
can confirm this indeed happens with Also found a very hacky workaround if you don't want to replace |
One thing you can try on v5 is to disable our batching and see if that helps:
see: https://tanstack.com/query/latest/docs/reference/notifyManager#notifymanagersetscheduler |
I would like to recommend changing the default setting of the networkMode configuration in React Query to 'always'. Given the library's widespread use, with over 2 million downloads per week, the current default setting may lead to significant issues. Recently, we encountered a bug where the system did not make any network requests due to the networkMode being set to 'online'. This issue arose because the browser's navigator.onLine API inaccurately reported the network status as offline, even though the connection was active. The impact of this bug is particularly severe as it can cause key pages to remain stuck on a loading screen without any indication of the problem. Considering that React Query is a critical part of many projects, such errors can have drastic consequences, including potential job losses, especially if key stakeholders encounter these issues firsthand. It can be challenging to identify this issue without delving into the source code, making it a silent but deadly problem. To enhance reliability and prevent such problematic scenarios, I strongly urge you to consider setting the default networkMode to 'always'. This change would ensure that network requests are made regardless of the navigator.onLine status, thus providing a more consistent and dependable user experience. Thank you for considering this suggestion. I believe it will significantly improve the robustness of React Query and safeguard many developers and businesses from facing similar issues. |
Hey @TkDodo |
you can set the scheduler inside cypress tests only. |
Hey @TkDodo However, adding it like this
or this
Doesn't work. Only works if directly added to the component |
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 withisLoading
astrue
anddata
asundefined
. Reverting back to react-query 2 fixed the problems.Most of the requests in our Cypress tests are stubbed using
cy.server
andcy.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:
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):
Additional context
Happens on both Cypress version 5.3.0 and 6.2.1.
The text was updated successfully, but these errors were encountered: