-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
[react-query] useSuspenseQueries does not throw on errors after queryKey change #6392
Comments
I can confirm that this is a bug. I need to look into it to find out what this is about. interestingly, it works fine for a normal
so it must be related to how / when suspense re-mounts the component 🤔 |
Thanks for confirming. A super simple possible solution could be to add something along the lines of: if (queryResult.data === undefined && queryResult.error) {
throw queryResult.error
} to useSuspenseQuery, instead of directly returning the result from |
Thanks, I have a fix already (two potential fixes actually) and will finish it in the next couple of days. |
setting options right before suspending is a side effect, and it has repercussions if used in transitions where the component stays mounted to fix #6392 in a different way, we can't rely on `observer.getCurrentQuery()` when reading for error boundaries / suspense, because it can lag behind; instead, we can just read from the queryCache directly to get the latest values
* fix(react): don't set options before suspending setting options right before suspending is a side effect, and it has repercussions if used in transitions where the component stays mounted to fix #6392 in a different way, we can't rely on `observer.getCurrentQuery()` when reading for error boundaries / suspense, because it can lag behind; instead, we can just read from the queryCache directly to get the latest values * chore: fix prettier * chore: test for 6486
Describe the bug
useSuspenseQuery
does not throw errors occurring in queryFn and returns data as undefined, when queryFn was invoked in reaction to a state change.See example and repro steps for more information.
Your minimal, reproducible example
https://codesandbox.io/s/react-query-usesuspensequery-does-not-throw-on-querykey-change-lpgyl9
Steps to reproduce
data
set toundefined
anderror
to the error thrown inqueryFn
In my minimal example, clicking the "Should fail" button toggles a
shouldFail
flag to true, makingqueryFn
throw an error, resulting in the described behavior. Clicking the button a second time makesuseSuspenseQuery
throw and trigger the ErrorBoundary.Expected behavior
I would expect
useSuspenseQuery
to throw when there was no data available for the current queryKey, not for the previous invocation.How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
Windows, behavior observed in both Firefox and Chrome.
Tanstack Query adapter
react-query
TanStack Query version
5.8.4
TypeScript version
No response
Additional context
I've done some digging myself, looking at
defaultThrowOnError
and noticed thatquery.queryKey
matches the previous query key, rather than the current and the data that indeed belongs to that previous queryKey. This data is not undefined, and thus no throw happens.The text was updated successfully, but these errors were encountered: