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