Skip to content

Commit

Permalink
fix(useQuery): don't throw error if errorBoundary has just been reset (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
TkDodo committed Nov 12, 2021
1 parent d3d7fc4 commit 2c36b6d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
59 changes: 59 additions & 0 deletions src/react/tests/QueryResetErrorBoundary.test.tsx
Expand Up @@ -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 (
<div>
<button onClick={() => refetch()}>refetch</button>
<div>status: {status}</div>
<div>{data}</div>
</div>
)
}

const rendered = renderWithClient(
queryClient,
<QueryErrorResetBoundary>
{({ reset }) => (
<ErrorBoundary
onReset={reset}
fallbackRender={({ resetErrorBoundary }) => (
<div>
<div>error boundary</div>
<button
onClick={() => {
resetErrorBoundary()
}}
>
retry
</button>
</div>
)}
>
<Page />
</ErrorBoundary>
)}
</QueryErrorResetBoundary>
)

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()

Expand Down
2 changes: 1 addition & 1 deletion src/react/useBaseQuery.ts
Expand Up @@ -131,7 +131,7 @@ export function useBaseQuery<
// Handle error boundary
if (
result.isError &&
defaultedOptions.enabled !== false &&
!errorResetBoundary.isReset() &&
!result.isFetching &&
shouldThrowError(
defaultedOptions.suspense,
Expand Down

1 comment on commit 2c36b6d

@vercel
Copy link

@vercel vercel bot commented on 2c36b6d Nov 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.