Open History to see the last commit
1---
2id: useMutationState
3title: useMutationState
4---
5β
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7β
8```ts
9function useMutationState<TResult = MutationState<unknown, Error, unknown, unknown>, TMutation extends Mutation<any, any, any, any> = MutationTypeFromResult<TResult>>(options?: MutationStateOptions<TResult, TMutation>, queryClient?: QueryClient): TResult[];
10```
11β
12Defined in: [packages/svelte-query/src/useMutationState.svelte.ts:103](https://github.com/TanStack/query/blob/main/packages/svelte-query/src/useMutationState.svelte.ts#L103)
13β
14`useMutationState` is a function 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`](../type-aliases/MutationTypeFromResult.md)\<`TResult`\>
27β
28## Parameters
29β
30### options?
31β
32[`MutationStateOptions`](../type-aliases/MutationStateOptions.md)\<`TResult`, `TMutation`\> = `{}`
33β
34The `filters` to narrow down matched mutations, and an optional `select` to transform the
35mutation state.
36β
37<a id="options-properties"></a>
38β
39#### `options` properties
40β
41| Property | Type | Description |
42| ------ | ------ | ------ |
43| <a id="options-property-filters"></a> `filters?` | [`MutationFilters`](../interfaces/MutationFilters.md) | The filters that select the mutations to return the state of. |
44| <a id="options-property-select"></a> `select?` | (`mutation`: `TMutation`) => `TResult` | Maps each matching mutation to the value returned for it. Defaults to the mutation's `state`. |
45β
46### queryClient?
47β
48[`QueryClient`](../classes/QueryClient.md)
49β
50Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
51be used.
52β
53## Returns
54β
55`TResult`[]
56β
57An Array of whatever `select` returns for each matching mutation.
58β
59## Examples
60β
61Get all variables of all running mutations:
62```svelte
63<script lang="ts">
64 import { useMutationState } from '@tanstack/svelte-query'
65β
66 const pendingVariables = useMutationState({
67 filters: { status: 'pending' },
68 select: (mutation) => mutation.state.variables,
69 })
70</script>
71β
72{pendingVariables.length} posts saving...
73```
74β
75Get all data for specific mutations via the `mutationKey`:
76```svelte
77<script lang="ts">
78 import { createMutation, useMutationState } from '@tanstack/svelte-query'
79β
80 const mutationKey = ['posts']
81β
82 // Some mutation that we want to get the state for
83 const mutation = createMutation(() => ({
84 mutationKey,
85 mutationFn: createPosts,
86 }))
87β
88 const savedPosts = useMutationState({
89 // this mutation key needs to match the mutation key of the given mutation (see above)
90 filters: { mutationKey, status: 'success' },
91 select: (mutation) => mutation.state.data,
92 })
93</script>
94β
95<button onclick={() => mutation.mutate(['New Post'])}>
96 Create post ({savedPosts.length} saved so far)
97</button>
98```
99β
100Access the latest mutation data via the `mutationKey`. Each invocation of `mutate` adds a new entry to the
101mutation cache for `gcTime` milliseconds β check the last item that `useMutationState` returns to get the
102latest successful mutation (the `status: 'success'` filter above excludes pending/errored ones):
103```svelte
104<script lang="ts">
105 import { useMutationState } from '@tanstack/svelte-query'
106β
107 const savedPosts = useMutationState({
108 filters: { mutationKey: ['posts'], status: 'success' },
109 select: (mutation) => mutation.state.data,
110 })
111β
112 const latestSavedPost = $derived(savedPosts[savedPosts.length - 1])
113</script>
114β
115{latestSavedPost ? 'Saved' : 'Nothing saved yet'}
116```