Open History to see the last commit
1---
2id: useMutationState
3title: useMutationState
4redirect_from:
5 - framework/react/reference/useMutationState
6---
7β
8```ts
9function useMutationState<TResult, TMutation>(options?: MutationStateOptions<TResult, TMutation>, queryClient?: QueryClient): TResult[];
10```
11β
12Defined in: [packages/react-query/src/useMutationState.ts:158](https://github.com/TanStack/query/blob/main/packages/react-query/src/useMutationState.ts#L158)
13β
14`useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass
15`filters` ([MutationFilters](../interfaces/MutationFilters.md)) to narrow down your mutations, and `select` to transform the mutation
16state.
17β
18## Type Parameters
19β
20### TResult
21β
22`TResult` = [`MutationState`](../interfaces/MutationState.md)\<`unknown`, `Error`, `unknown`, `unknown`\>
23β
24### TMutation
25β
26`TMutation` *extends* [`Mutation`](../classes/Mutation.md)\<`any`, `any`, `any`, `any`\> = `MutationTypeFromResult`\<`TResult`\>
27β
28## Parameters
29β
30### options?
31β
32`MutationStateOptions`\<`TResult`, `TMutation`\> = `{}`
33β
34The `filters` to narrow down matched mutations, and an optional `select` to transform the
35mutation state.
36β
37### queryClient?
38β
39[`QueryClient`](../classes/QueryClient.md)
40β
41Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
42be used.
43β
44## Returns
45β
46`TResult`[]
47β
48Will be an Array of whatever `select` returns for each matching mutation.
49β
50## Examples
51β
52Get all variables of all running mutations:
53```tsx
54import { useMutationState } from '@tanstack/react-query'
55β
56function PendingPosts() {
57 const pendingVariables = useMutationState({
58 filters: { status: 'pending' },
59 select: (mutation) => mutation.state.variables,
60 })
61β
62 return <>{pendingVariables.length} posts saving...</>
63}
64```
65β
66Get all data for specific mutations via the `mutationKey`:
67```tsx
68import { useMutation, useMutationState } from '@tanstack/react-query'
69β
70const mutationKey = ['posts']
71β
72function Posts() {
73 // Some mutation that we want to get the state for
74 const mutation = useMutation({
75 mutationKey,
76 mutationFn: createPosts,
77 })
78β
79 const savedPosts = useMutationState({
80 // this mutation key needs to match the mutation key of the given mutation (see above)
81 filters: { mutationKey, status: 'success' },
82 select: (mutation) => mutation.state.data,
83 })
84β
85 return (
86 <button onClick={() => mutation.mutate(['New Post'])}>
87 Create post ({savedPosts.length} saved so far)
88 </button>
89 )
90}
91```
92β
93Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new
94entry to the mutation cache for `gcTime` milliseconds β with the `status: 'success'` filter below, check the
95last item that `useMutationState` returns to get the latest successful invocation:
96```tsx
97import { useMutationState } from '@tanstack/react-query'
98β
99function LatestPost() {
100 const savedPosts = useMutationState({
101 filters: { mutationKey: ['posts'], status: 'success' },
102 select: (mutation) => mutation.state.data,
103 })
104β
105 const latestSavedPost = savedPosts[savedPosts.length - 1]
106β
107 return <>{latestSavedPost ? 'Saved' : 'Nothing saved yet'}</>
108}
109```