Skip to content
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

Exposing errorUpdateCount on useQuery #3532

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions docs/src/pages/reference/useQuery.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ const result = useQuery({
- The failure count for the query.
- Incremented every time the query fails.
- Reset to `0` when the query succeeds.
- `errorUpdateCount: number`
- The sum of all errors.
- `refetch: (options: { throwOnError: boolean, cancelRefetch: boolean }) => Promise<UseQueryResult>`
- A function to manually refetch the query.
- If the query errors, the error will only be logged. If you want an error to be thrown, pass the `throwOnError: true` option
Expand Down
1 change: 1 addition & 0 deletions src/core/queryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ export class QueryObserver<
error,
errorUpdatedAt,
failureCount: state.fetchFailureCount,
errorUpdateCount: state.errorUpdateCount,
TkDodo marked this conversation as resolved.
Show resolved Hide resolved
isFetched: state.dataUpdateCount > 0 || state.errorUpdateCount > 0,
isFetchedAfterMount:
state.dataUpdateCount > queryInitialState.dataUpdateCount ||
Expand Down
1 change: 1 addition & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ export interface QueryObserverBaseResult<TData = unknown, TError = unknown> {
error: TError | null
errorUpdatedAt: number
failureCount: number
errorUpdateCount: number
isError: boolean
isFetched: boolean
isFetchedAfterMount: boolean
Expand Down
33 changes: 33 additions & 0 deletions src/react/tests/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4677,4 +4677,37 @@ describe('useQuery', () => {

consoleMock.mockRestore()
})

it('errorUpdateCount should increased on each refetch', async () => {
const consoleMock = mockConsoleError()
const key = queryKey()
const error = new Error('oops')

function Page() {
const { refetch, errorUpdateCount } = useQuery(
key,
async () => {
throw error;
},
{
retry: false,
}
)
return <div>
<button onClick={() => refetch()}>refetch</button>
<span>
data: {errorUpdateCount}
</span>
</div>
}
const rendered = renderWithClient(queryClient, <Page />)
const fetchBtn = rendered.getByRole('button', { name: 'refetch' })
await waitFor(() => rendered.getByText('data: 1'))
fireEvent.click(fetchBtn)
await waitFor(() => rendered.getByText('data: 2'))
fireEvent.click(fetchBtn)
await waitFor(() => rendered.getByText('data: 3'))

consoleMock.mockRestore()
})
})