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

useMutationState.md

122 lines3.7 KB

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```