Dependent Query Error Handling in Custom Hook #889
Replies: 4 comments 1 reply
-
|
did you ever find a solution to this @josephliccini. I tried writing some wrapper hooks to deal with this problem and ran into stale closure issues. If I come up with a solution I'll post it here but it has been more challenging than expected. |
Beta Was this translation helpful? Give feedback.
-
|
I think I would just check if an error exists in the parent query and if it does, return the parent query with the error instead of the child query. Another idea would be to allow the dependent query to run even when a parent error occurs, but just have it error with its own error. |
Beta Was this translation helpful? Give feedback.
-
|
I've been using react-query for some years now. I've used dependent queries before as a means of transforming data from the API response (not doing a new fetch in the dependent query). If you have stumbled on this thread with an issue like mine, using the select API is probably what you need. Covered nicely in this blog post. |
Beta Was this translation helpful? Give feedback.
-
export function useCustomQuery() {
const firstQuery = useQuery({ queryKey: ['first'], queryFn: fetchFirstThing })
const secondQuery = useQuery({ enabled: !!firstQuery.data, queryKey: ['second'], queryFn: fetchSecondThing })
if (firstQuery.isError) return firstQuery
return secondQuery
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
How can I best notify a consumer of a custom hook that an error has occurred in a dependent query, and should show an error state?
Here's an example:
If
isError/errorare populated anddatadoesn't resolve in the firstuseQuery, what is the suggested way to notify the consumer of thisuseRemoteDatahook that an error has occurred, and they should use an error UI.The second
useQueryin this example will always remain inidlestate, the the consumer will not be notified of the dependency's error.I suppose one could, before returning the second
useQuery, do a check across all dependencies for a query and if they are in error, return aQueryInfotype withisErrorset totrueanderrorset to one of the exceptions, but this could blow up if there are multiple dependencies required to execute the finaluseQuery.Beta Was this translation helpful? Give feedback.
All reactions