From 2c36b6d9e9cdfcbcacff640e8782274c8e7f6e30 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Fri, 12 Nov 2021 22:11:12 +0100 Subject: [PATCH] fix(useQuery): don't throw error if errorBoundary has just been reset (#2935) the fix for disabled queries was wrong, because disabled queries still need to throw if they are fetching due to some other means, like `refetch`. However, if a query has just been reset, we want to skip throwing for one render cycle. The useEffect that clears the reset will then make sure that further errors will be thrown --- .../tests/QueryResetErrorBoundary.test.tsx | 59 +++++++++++++++++++ src/react/useBaseQuery.ts | 2 +- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/react/tests/QueryResetErrorBoundary.test.tsx b/src/react/tests/QueryResetErrorBoundary.test.tsx index 1ee596669e..f73210d251 100644 --- a/src/react/tests/QueryResetErrorBoundary.test.tsx +++ b/src/react/tests/QueryResetErrorBoundary.test.tsx @@ -204,6 +204,65 @@ describe('QueryErrorResetBoundary', () => { consoleMock.mockRestore() }) + it('should throw error if query is disabled and manually refetched', async () => { + const key = queryKey() + + const consoleMock = mockConsoleError() + + function Page() { + const { data, refetch, status } = useQuery( + key, + async () => { + throw new Error('Error') + }, + { + retry: false, + enabled: false, + useErrorBoundary: true, + } + ) + + return ( +
+ +
status: {status}
+
{data}
+
+ ) + } + + const rendered = renderWithClient( + queryClient, + + {({ reset }) => ( + ( +
+
error boundary
+ +
+ )} + > + +
+ )} +
+ ) + + await waitFor(() => rendered.getByText('status: idle')) + rendered.getByRole('button', { name: /refetch/i }).click() + await waitFor(() => rendered.getByText('error boundary')) + + consoleMock.mockRestore() + }) + it('should not retry fetch if the reset error boundary has not been reset', async () => { const key = queryKey() diff --git a/src/react/useBaseQuery.ts b/src/react/useBaseQuery.ts index 149d80320c..693944fc38 100644 --- a/src/react/useBaseQuery.ts +++ b/src/react/useBaseQuery.ts @@ -131,7 +131,7 @@ export function useBaseQuery< // Handle error boundary if ( result.isError && - defaultedOptions.enabled !== false && + !errorResetBoundary.isReset() && !result.isFetching && shouldThrowError( defaultedOptions.suspense,