Open History to see the last commit
1---
2id: useMutationState
3title: useMutationState
4redirect_from:
5 - framework/vue/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?:
12 | MutationStateOptions<TResult, TMutation>
13| (() => MutationStateOptions<TResult, TMutation>), queryClient?: QueryClient): Readonly<Ref<TResult[]>>;
14```
15β
16Defined in: [packages/vue-query/src/useMutationState.ts:211](https://github.com/TanStack/query/blob/main/packages/vue-query/src/useMutationState.ts#L211)
17β
18`useMutationState` is a composable that gives you access to all mutations in the `MutationCache`. You can
19pass `filters` ([MutationFilters](../interfaces/MutationFilters.md)) to narrow down your mutations, and `select` to transform the
20mutation state.
21β
22`options` may be a plain object or a reactive getter (`() => ({ ... })`) β pass a getter if the filters
23themselves depend on other reactive state.
24β
25## Type Parameters
26β
27### TResult
28β
29`TResult` = [`MutationState`](../interfaces/MutationState.md)\<`unknown`, `Error`, `unknown`, `unknown`\>
30β
31### TMutation
32β
33`TMutation` *extends* [`Mutation`](../classes/Mutation.md)\<`any`, `any`, `any`, `any`\> = `MutationTypeFromResult`\<`TResult`\>
34β
35## Parameters
36β
37### options?
38β
39 \| [`MutationStateOptions`](../type-aliases/MutationStateOptions.md)\<`TResult`, `TMutation`\>
40 \| (() => [`MutationStateOptions`](../type-aliases/MutationStateOptions.md)\<`TResult`, `TMutation`\>) = `{}`
41β
42The `filters` to narrow down matched mutations, and an optional `select` to transform the
43mutation state.
44β
45<a id="options-properties"></a>
46β
47#### `options` properties
48β
49Built from [`MutationStateOptions`](../type-aliases/MutationStateOptions.md#properties). See the type above for what it changes.
50β
51### queryClient?
52β
53[`QueryClient`](../classes/QueryClient.md)
54β
55Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
56will be used.
57β
58## Returns
59β
60`Readonly`\<`Ref`\<`TResult`[]\>\>
61β
62A `ref` to an Array of whatever `select` returns for each matching mutation.
63β
64## Examples
65β
66Get all variables of all running mutations:
67```vue
68<script setup lang="ts">
69import { useMutationState } from '@tanstack/vue-query'
70β
71const pendingVariables = useMutationState({
72 filters: { status: 'pending' },
73 select: (mutation) => mutation.state.variables,
74})
75</script>
76β
77<template>{{ pendingVariables.length }} posts saving...</template>
78```
79β
80Get all data for specific mutations via the `mutationKey`:
81```vue
82<script setup lang="ts">
83import { useMutation, useMutationState } from '@tanstack/vue-query'
84β
85const mutationKey = ['posts']
86β
87// Some mutation that we want to get the state for
88const mutation = useMutation({
89 mutationKey,
90 mutationFn: createPosts,
91})
92β
93const savedPosts = useMutationState({
94 // this mutation key needs to match the mutation key of the given mutation (see above)
95 filters: { mutationKey, status: 'success' },
96 select: (mutation) => mutation.state.data,
97})
98</script>
99β
100<template>
101 <button @click="mutation.mutate(['New Post'])">
102 Create post ({{ savedPosts.length }} saved so far)
103 </button>
104</template>
105```
106β
107Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new
108entry to the mutation cache for `gcTime` milliseconds β with the `status: 'success'` filter below, check the
109last item that `useMutationState` returns to get the latest successful invocation:
110```vue
111<script setup lang="ts">
112import { computed } from 'vue'
113import { useMutationState } from '@tanstack/vue-query'
114β
115const savedPosts = useMutationState({
116 filters: { mutationKey: ['posts'], status: 'success' },
117 select: (mutation) => mutation.state.data,
118})
119β
120const latestSavedPost = computed(() => savedPosts.value[savedPosts.value.length - 1])
121</script>
122β
123<template>{{ latestSavedPost ? 'Saved' : 'Nothing saved yet' }}</template>
124```