Skip to content

Commit 369d826

Browse files
authored
docs: access latest data for useMutationState (#7738)
1 parent 79e5202 commit 369d826

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

docs/framework/react/reference/useMutationState.md

+29
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,35 @@ const data = useMutationState({
3636
filters: { mutationKey },
3737
select: (mutation) => mutation.state.data,
3838
})
39+
40+
```
41+
42+
**Example 3: Access the latest mutation data via the `mutationKey`**
43+
Each invocation of `mutate` adds a new entry to the mutation cache for `gcTime` milliseconds.
44+
45+
To access the latest invocation, you can check for the last item that `useMutationState` returns.
46+
47+
```tsx
48+
import { useMutation, useMutationState } from '@tanstack/react-query'
49+
50+
const mutationKey = ['posts']
51+
52+
// Some mutation that we want to get the state for
53+
const mutation = useMutation({
54+
mutationKey,
55+
mutationFn: (newPost) => {
56+
return axios.post('/posts', newPost)
57+
},
58+
})
59+
60+
const data = useMutationState({
61+
// this mutation key needs to match the mutation key of the given mutation (see above)
62+
filters: { mutationKey },
63+
select: (mutation) => mutation.state.data,
64+
})
65+
66+
// Latest mutation data
67+
const latest = data[data.length - 1]
3968
```
4069

4170
**Options**

0 commit comments

Comments
 (0)