Skip to content

Commit d98b8fd

Browse files
add doc
1 parent 84b7fc5 commit d98b8fd

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

docs/eslint/no-mutation-in-deps.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
id: no-mutation-in-deps
3+
title: Disallow putting the result of useMutation directly in a React hook dependency array
4+
---
5+
6+
The object returned from `useMutation` is **not** referentially stable, so it should **not** be put directly into the dependency array of a React hook (e.g. `useEffect`, `useMemo`, `useCallback`).
7+
Instead, destructure the return value of useMutation and pass the destructured values into the dependency array.
8+
9+
## Rule Details
10+
11+
Examples of **incorrect** code for this rule:
12+
13+
```tsx
14+
/* eslint "@tanstack/query/no-mutation-in-deps": "warn" */
15+
import { useCallback } from 'React'
16+
import { useMutation } from '@tanstack/react-query'
17+
18+
function Component() {
19+
const mutation = useMutation({ mutationFn: (value: string) => value })
20+
const callback = useCallback(() => {
21+
mutation.mutate('hello')
22+
}, [mutation])
23+
return null
24+
}
25+
```
26+
27+
Examples of **correct** code for this rule:
28+
29+
```tsx
30+
/* eslint "@tanstack/query/no-mutation-in-deps": "warn" */
31+
import { useCallback } from 'React'
32+
import { useMutation } from '@tanstack/react-query'
33+
34+
function Component() {
35+
const { mutate } = useMutation({ mutationFn: (value: string) => value })
36+
const callback = useCallback(() => {
37+
mutate('hello')
38+
}, [mutate])
39+
return null
40+
}
41+
```
42+
43+
## Attributes
44+
45+
- [x] ✅ Recommended
46+
- [ ] 🔧 Fixable

0 commit comments

Comments
 (0)