Skip to content

fix(react-query): allow retryOnMount when throwOnError is function (#9336) #9338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions packages/react-query/src/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6027,6 +6027,7 @@ describe('useQuery', () => {
it('should be able to toggle subscribed', async () => {
const key = queryKey()
const queryFn = vi.fn(() => Promise.resolve('data'))

function Page() {
const [subscribed, setSubscribed] = React.useState(true)
const { data } = useQuery({
Expand Down Expand Up @@ -6069,6 +6070,7 @@ describe('useQuery', () => {
it('should not be attached to the query when subscribed is false', async () => {
const key = queryKey()
const queryFn = vi.fn(() => Promise.resolve('data'))

function Page() {
const { data } = useQuery({
queryKey: key,
Expand All @@ -6095,6 +6097,7 @@ describe('useQuery', () => {
it('should not re-render when data is added to the cache when subscribed is false', async () => {
const key = queryKey()
let renders = 0

function Page() {
const { data } = useQuery({
queryKey: key,
Expand Down Expand Up @@ -6297,6 +6300,7 @@ describe('useQuery', () => {
await sleep(5)
return { numbers: { current: { id } } }
}

function Test() {
const [id, setId] = React.useState(1)

Expand Down Expand Up @@ -6357,6 +6361,7 @@ describe('useQuery', () => {
await sleep(5)
return { numbers: { current: { id } } }
}

function Test() {
const [id, setId] = React.useState(1)

Expand Down Expand Up @@ -6854,10 +6859,12 @@ describe('useQuery', () => {
it('should console.error when there is no queryFn', () => {
const consoleErrorMock = vi.spyOn(console, 'error')
const key = queryKey()

function Example() {
useQuery({ queryKey: key })
return <></>
}

renderWithClient(queryClient, <Example />)

expect(consoleErrorMock).toHaveBeenCalledTimes(1)
Expand All @@ -6867,4 +6874,56 @@ describe('useQuery', () => {

consoleErrorMock.mockRestore()
})

it('should retry on mount when throwOnError returns false', async () => {
const key = queryKey()
let fetchCount = 0
const queryFn = vi.fn().mockImplementation(() => {
fetchCount++
console.log(`Fetching... (attempt ${fetchCount})`)
return Promise.reject(new Error('Simulated 500 error'))
})

function Component() {
const { status, error } = useQuery({
queryKey: key,
queryFn,
throwOnError: () => false,
retryOnMount: true,
staleTime: Infinity,
retry: false,
})

return (
<div>
<div data-testid="status">{status}</div>
{error && <div data-testid="error">{error.message}</div>}
</div>
)
}

const { unmount, getByTestId } = renderWithClient(
queryClient,
<Component />,
)

await vi.waitFor(() =>
expect(getByTestId('status')).toHaveTextContent('error'),
)
expect(getByTestId('error')).toHaveTextContent('Simulated 500 error')
expect(fetchCount).toBe(1)

unmount()

const initialFetchCount = fetchCount

renderWithClient(queryClient, <Component />)

await vi.waitFor(() =>
expect(getByTestId('status')).toHaveTextContent('error'),
)

expect(fetchCount).toBe(initialFetchCount + 1)
expect(queryFn).toHaveBeenCalledTimes(2)
})
})
5 changes: 4 additions & 1 deletion packages/react-query/src/errorBoundaryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ export const ensurePreventErrorBoundaryRetry = <
) => {
if (
options.suspense ||
options.throwOnError ||
options.experimental_prefetchInRender
) {
// Prevent retrying failed query if the error boundary has not been reset yet
if (!errorResetBoundary.isReset()) {
options.retryOnMount = false
}
} else if (options.throwOnError) {
if (!errorResetBoundary.isReset() && typeof options.throwOnError !== 'function') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The function can return true or false so I think we'd need to evaluate it to get the result

Copy link
Author

Choose a reason for hiding this comment

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

Updated to evaluate the throwOnError function result instead of just checking if it's a function. Thanks for the feedback!

options.retryOnMount = false
}
}
}

Expand Down