Open History to see the last commit
1---
2id: useMutationState
3title: useMutationState
4redirect_from:
5 - framework/solid/reference/useMutationState
6---
7β
8```ts
9function useMutationState<TResult, TMutation>(options?: Accessor<MutationStateOptions<TResult, TMutation>>, queryClient?: Accessor<QueryClient>): Accessor<TResult[]>;
10```
11β
12Defined in: [packages/solid-query/src/useMutationState.ts:127](https://github.com/TanStack/query/blob/main/packages/solid-query/src/useMutationState.ts#L127)
13β
14`useMutationState` is a primitive 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`Accessor`\<`MutationStateOptions`\<`TResult`, `TMutation`\>\> = `...`
33β
34An accessor returning the `filters` to narrow down matched mutations, and an optional
35`select` to transform the mutation state.
36β
37### queryClient?
38β
39`Accessor`\<[`QueryClient`](../classes/QueryClient.md)\>
40β
41An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
42will be used.
43β
44## Returns
45β
46`Accessor`\<`TResult`[]\>
47β
48An accessor for 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/solid-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/solid-query'
69β
70const mutationKey = ['posts']
71β
72function Posts() {
73 // Some mutation that we want to get the state for
74 const createPostsMutation = 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={() => createPostsMutation.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/solid-query'
98β
99function LatestPost() {
100 const savedPosts = useMutationState(() => ({
101 filters: { mutationKey: ['posts'], status: 'success' },
102 select: (mutation) => mutation.state.data,
103 }))
104β
105 const latestPost = () => savedPosts()[savedPosts().length - 1]
106β
107 return <span>{latestPost()?.title}</span>
108}
109```