-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84b7fc5
commit d98b8fd
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
id: no-mutation-in-deps | ||
title: Disallow putting the result of useMutation directly in a React hook dependency array | ||
--- | ||
|
||
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`). | ||
Instead, destructure the return value of useMutation and pass the destructured values into the dependency array. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```tsx | ||
/* eslint "@tanstack/query/no-mutation-in-deps": "warn" */ | ||
import { useCallback } from 'React' | ||
import { useMutation } from '@tanstack/react-query' | ||
|
||
function Component() { | ||
const mutation = useMutation({ mutationFn: (value: string) => value }) | ||
const callback = useCallback(() => { | ||
mutation.mutate('hello') | ||
}, [mutation]) | ||
return null | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```tsx | ||
/* eslint "@tanstack/query/no-mutation-in-deps": "warn" */ | ||
import { useCallback } from 'React' | ||
import { useMutation } from '@tanstack/react-query' | ||
|
||
function Component() { | ||
const { mutate } = useMutation({ mutationFn: (value: string) => value }) | ||
const callback = useCallback(() => { | ||
mutate('hello') | ||
}, [mutate]) | ||
return null | ||
} | ||
``` | ||
|
||
## Attributes | ||
|
||
- [x] ✅ Recommended | ||
- [ ] 🔧 Fixable |