Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useMutationState.md

114 lines3.6 KB

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