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

  1. Visit the Code Sandbox
  2. Open the console
  3. 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)
  4. Press the “Hide” button to unmount the ErrorBoundary
  5. Press the “Show” button to render it again
  6. 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

Most upvoted comments

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 call reset() 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 QueryErrorResetBoundary component. 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 need retryOnMount for this. It’s a weird flag, because it also doesn’t have anything to do with “retries”. It’s a refetch, but refetchOnMount is 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:

const queryClient = new QueryClient({
  queryCache: new QueryCache({
    onError(error, query) {
      setTimeout(() => {
        queryClient.removeQueries(query.queryKey);
      }, 1000);
    }
  }),
  defaultOptions: {
    queries: {
      retry: 1,
      suspense: true,
      useErrorBoundary: true
    }
  }
});

With retryOnMount set to true I was expecting that simply clearing the error state in a boundary thereby causing a remount of children would do the trick for retrying

I thought so too, but alas, it’s not that simple. In my mental modal, what should happen is:

  • throw error
  • error boundary renders
  • error boundary unmounts, children render, fetch again

what actually happens is:

  • throw error
  • component renders again (!)
  • then, error boundary renders

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 resetKeys not 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:

  • use FallbackComponent instead of fallback
  • in the unmount useEffect, we have the reset both the errorBoundary from react-error-boundary as well as the one from react-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 use resetKeys, as this example shows: