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