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

useMutationState.md

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