Skip to content

Commit 578e417

Browse files
committed
reset error state when executing useMutation's reset
1 parent 9f3b2e9 commit 578e417

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

.size-snapshot.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"dist/index.js": {
3-
"bundled": 31764,
4-
"minified": 16087,
5-
"gzipped": 4618
3+
"bundled": 31777,
4+
"minified": 16088,
5+
"gzipped": 4619
66
},
77
"dist/index.es.js": {
8-
"bundled": 31223,
9-
"minified": 15604,
10-
"gzipped": 4516,
8+
"bundled": 31236,
9+
"minified": 15605,
10+
"gzipped": 4517,
1111
"treeshaked": {
1212
"rollup": {
1313
"code": 3329,

src/__tests__/useMutation-test.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
waitForElement,
66
} from '@testing-library/react'
77
import * as React from 'react'
8-
import { act } from 'react-dom/test-utils'
98

109
import { useMutation } from '../index'
1110

@@ -43,4 +42,39 @@ describe('useMutation', () => {
4342

4443
expect(getByTestId('title').textContent).toBe('')
4544
})
45+
46+
it('should be able to reset `error`', async () => {
47+
function Page() {
48+
const [mutate, mutationResult] = useMutation(
49+
() => Promise.reject(new Error('something went wrong')),
50+
{
51+
throwOnError: false
52+
}
53+
)
54+
55+
return (
56+
<div>
57+
{mutationResult.error &&
58+
<h1 data-testid="error">{mutationResult.error.message}</h1>
59+
}
60+
<button onClick={mutationResult.reset}>reset</button>
61+
<button onClick={mutate}>mutate</button>
62+
</div>
63+
)
64+
}
65+
66+
const { getByTestId, getByText, queryByTestId } = render(<Page />)
67+
68+
expect(queryByTestId('error')).toBeNull()
69+
70+
fireEvent.click(getByText('mutate'))
71+
72+
await waitForElement(() => getByTestId('error'))
73+
74+
expect(getByTestId('error').textContent).toBe('something went wrong')
75+
76+
fireEvent.click(getByText('reset'))
77+
78+
expect(queryByTestId('error')).toBeNull()
79+
})
4680
})

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,10 @@ export function useMutation(
697697
[refetchQueries, refetchQueriesOnFailure, throwOnError]
698698
)
699699

700-
const reset = React.useCallback(() => setData(null), [])
700+
const reset = React.useCallback(() => {
701+
setData(null)
702+
setError(null)
703+
}, [])
701704

702705
React.useEffect(() => {
703706
if (useErrorBoundary && error) {

0 commit comments

Comments
 (0)