Skip to content

Commit 2c36b6d

Browse files
authored
fix(useQuery): don't throw error if errorBoundary has just been reset (TanStack#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
1 parent d3d7fc4 commit 2c36b6d

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/react/tests/QueryResetErrorBoundary.test.tsx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,65 @@ describe('QueryErrorResetBoundary', () => {
204204
consoleMock.mockRestore()
205205
})
206206

207+
it('should throw error if query is disabled and manually refetched', async () => {
208+
const key = queryKey()
209+
210+
const consoleMock = mockConsoleError()
211+
212+
function Page() {
213+
const { data, refetch, status } = useQuery(
214+
key,
215+
async () => {
216+
throw new Error('Error')
217+
},
218+
{
219+
retry: false,
220+
enabled: false,
221+
useErrorBoundary: true,
222+
}
223+
)
224+
225+
return (
226+
<div>
227+
<button onClick={() => refetch()}>refetch</button>
228+
<div>status: {status}</div>
229+
<div>{data}</div>
230+
</div>
231+
)
232+
}
233+
234+
const rendered = renderWithClient(
235+
queryClient,
236+
<QueryErrorResetBoundary>
237+
{({ reset }) => (
238+
<ErrorBoundary
239+
onReset={reset}
240+
fallbackRender={({ resetErrorBoundary }) => (
241+
<div>
242+
<div>error boundary</div>
243+
<button
244+
onClick={() => {
245+
resetErrorBoundary()
246+
}}
247+
>
248+
retry
249+
</button>
250+
</div>
251+
)}
252+
>
253+
<Page />
254+
</ErrorBoundary>
255+
)}
256+
</QueryErrorResetBoundary>
257+
)
258+
259+
await waitFor(() => rendered.getByText('status: idle'))
260+
rendered.getByRole('button', { name: /refetch/i }).click()
261+
await waitFor(() => rendered.getByText('error boundary'))
262+
263+
consoleMock.mockRestore()
264+
})
265+
207266
it('should not retry fetch if the reset error boundary has not been reset', async () => {
208267
const key = queryKey()
209268

src/react/useBaseQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export function useBaseQuery<
131131
// Handle error boundary
132132
if (
133133
result.isError &&
134-
defaultedOptions.enabled !== false &&
134+
!errorResetBoundary.isReset() &&
135135
!result.isFetching &&
136136
shouldThrowError(
137137
defaultedOptions.suspense,

0 commit comments

Comments
 (0)