Cache query errors
Hello.
I wonder if there is an option that I could cache
I have extracted
useQuery
errors to account for staleTime
?I have extracted
useQuery
into separate hook and I am using it in several places. I am also displaying error when request fails in onError
callback of that useQuery. Now when re-used in several places it is causing the error to display several times.
I would have hoped the 'onError' would be called just once and there would be no re-fetches on windowFocus for given staleTime
.
Please advise.4 Replies
passive-yellow•3y ago
It might depend on exactly what error you're showing and where but I think what you're after is the global callback feature: https://tkdodo.eu/blog/react-query-error-handling
Passing a default onError to the query client query cache should invoke the callback once across all observers.
React Query Error Handling
After covering the sunshine cases of data fetching, it's time to look at situations where things don't go as planned and "Something went wrong..."
optimistic-gold•3y ago
Yeah, errors are observed by all observers (all instances of your hook) so that's expected. A global callback sounds sensible to me 👍
sensitive-blueOP•3y ago
This is interesting but it didn't solve the issue. It is still the case that when fetch fails and I switch tabs and go back the error occurs again and the error is displayed (regardless its global or local callback).
I will probably have to switch from throwing errors via react-query to catching it and mapping to some non-erroneous response that is cached and staled according to all the timers set on the query
passive-yellow•3y ago
Ya, that makes sense because it'll be refetching on refocus. While the global callback should only show one error at a time, it'll still behave normally in terms of refetching and therefore erroring too, not a once only and never again sorta deal.
I'm not sure of a way around that without removing refocus refetching or much larger stale times, which doesn't sound like it's what you're after. Maybe someone else has another suggestion! 😅