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