query: Errored queries caught by ErrorBoundary are not retried on mount
Describe the bug I’m using Suspense and ErrorBoundaries and I’m seeing unexpected behavior when queries are erroring. If a query errors I’m displaying an ErrorBoundary for that part of the UI. Now if I navigate to another page using react-router-dom and navigate back to the page, the ErrorBoundary is still there and the query is not retried. I know that normally you would have to reset the error boundary while you are on the same page to trigger a re-run of the failed queries, but in this case I would’ve assumed that the queries are retried when I visit the page again, i.e. the components are mounted again.
To Reproduce
- Visit the Code Sandbox
- Open the console
- Wait until the error occurs and the fallback of the ErrorBoundary is shown: “An error occured!” (You need to click off of react-error-overlay to see it)
- Press the “Hide” button to unmount the ErrorBoundary
- Press the “Show” button to render it again
- Query is not re-executed and the error is immediately shown again
Expected behavior
The query is re-executed after the component mounts, i.e. honoring the retryOnMount setting.
Desktop (please complete the following information):
- Version: 3.24.4
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 4
- Comments: 41
sadly no, the idea I had (like all other ideas to make this better) did not work out 😂
I have another (hacky) idea to try and get rid of the
QueryErrorResetBoundary. Will try to work on it this week.Tbh, I can see that there are a bunch of unexpected behaviours around
QueryErrorResetBoundary. I would fully expect that if we callreset()in a useEffect cleanup that it would reset the boundary, but it somehow doesn’t. If someone wants to take a look into this, please do.On a broader note: I actually wanted to get rid of the whole
QueryErrorResetBoundarycomponent. Imo, it would be best if everything could work without it. The idea is that if a component is in error state, we throw to the error boundary, and the next time the component renders, we would refetch, because the query is in error state. Actually, we also don’t needretryOnMountfor this. It’s a weird flag, because it also doesn’t have anything to do with “retries”. It’s a refetch, butrefetchOnMountis already a flag 🙃 .One of the problems I ran into was that react renders the component one more time after we already threw the error. If we were to fetch here again, it would result in an wrong refetch / potentially infinite loop. There was also a problem with suspense afair. But I think if we could get this done, it would be the best solution because there wouldn’t be a manual reset as far as Query is concerned at all.
If that sounds interesting for you to work on, please give it a go. I’m happy to give support but I won’t actively go down this rabbit hole for now 😃
@TkDodo Thanks for advice, I adjust the PoC codes:
I thought so too, but alas, it’s not that simple. In my mental modal, what should happen is:
what actually happens is:
https://twitter.com/TkDodo/status/1624720921842925574
the component re-rendering again in error state is pretty problematic for us, because we would immediately refetch. I tried to work around that, but then it kind of screwed
suspense. It’s pretty tricky.Anyways, did my above solution with
resetKeysnot solve the issue?looked into this again today because we’re also facing it now, here is an altered example that seems to work:
changes I made:
FallbackComponentinstead offallbackerrorBoundaryfromreact-error-boundaryas well as the one fromreact-query.One problem that remains is that each component needs to have its own “instance” of
ErrorFallback, because otherwise, the component merely re-renders and never unmounts, so the effect doesn’t run.If we want to use the same
FallbackComponent, we have to useresetKeys, as this example shows: