Open History to see the last commit
1---
2id: useMutationState
3title: useMutationState
4---
5β
6`useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass `filters` to it to narrow down your mutations, and `select` to transform the mutation state.
7β
8**Example 1: Get all variables of all running mutations**
9β
10```tsx
11import { useMutationState } from '@tanstack/react-query'
12β
13const variables = useMutationState({
14 filters: { status: 'pending' },
15 select: (mutation) => mutation.state.variables,
16})
17```
18β
19**Example 2: Get all data for specific mutations via the `mutationKey`**
20β
21```tsx
22import { useMutation, useMutationState } from '@tanstack/react-query'
23β
24const mutationKey = ['posts']
25β
26// Some mutation that we want to get the state for
27const mutation = useMutation({
28 mutationKey,
29 mutationFn: (newPost) => {
30 return axios.post('/posts', newPost)
31 },
32})
33β
34const data = useMutationState({
35 // this mutation key needs to match the mutation key of the given mutation (see above)
36 filters: { mutationKey },
37 select: (mutation) => mutation.state.data,
38})
39```
40β
41**Options**
42β
43- `options`
44 - `filters?: MutationFilters`: [Mutation Filters](./guides/filters#mutation-filters)
45 - `select?: (mutation: Mutation) => TResult`
46 - Use this to transform the mutation state.
47- `queryClient?: QueryClient`,
48 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
49β
50**Returns**
51β
52- `Array<TResult>`
53 - Will be an Array of whatever `select` returns for each matching mutation.