Open History to see the last commit
1---
2id: MutationCache
3title: MutationCache
4---
5β
6The `MutationCache` is the storage for mutations.
7β
8**Normally, you will not interact with the MutationCache directly and instead use the `QueryClient`.**
9β
10```tsx
11import { MutationCache } from '@tanstack/react-query'
12β
13const mutationCache = new MutationCache({
14 onError: error => {
15 console.log(error)
16 },
17 onSuccess: data => {
18 console.log(data)
19 },
20})
21```
22β
23Its available methods are:
24β
25- [`getAll`](#mutationcachegetall)
26- [`subscribe`](#mutationcachesubscribe)
27- [`clear`](#mutationcacheclear)
28β
29**Options**
30β
31- `onError?: (error: unknown, variables: unknown, context: unknown, mutation: Mutation) => Promise<unknown> | unknown`
32 - Optional
33 - This function will be called if some mutation encounters an error.
34 - If you return a Promise from it, it will be awaited
35- `onSuccess?: (data: unknown, variables: unknown, context: unknown, mutation: Mutation) => Promise<unknown> | unknown`
36 - Optional
37 - This function will be called if some mutation is successful.
38 - If you return a Promise from it, it will be awaited
39- `onMutate?: (variables: unknown, mutation: Mutation) => Promise<unknown> | unknown`
40 - Optional
41 - This function will be called before some mutation executes.
42 - If you return a Promise from it, it will be awaited
43β
44## Global callbacks
45β
46The `onError`, `onSuccess` and `onMutate` callbacks on the MutationCache can be used to handle these events on a global level. They are different to `defaultOptions` provided to the QueryClient because:
47β
48- `defaultOptions` can be overridden by each Mutation - the global callbacks will **always** be called.
49- `onMutate` does not allow returning a context value.
50β
51## `mutationCache.getAll`
52β
53`getAll` returns all mutations within the cache.
54β
55> Note: This is not typically needed for most applications, but can come in handy when needing more information about a mutation in rare scenarios
56β
57```tsx
58const mutations = mutationCache.getAll()
59```
60β
61**Returns**
62β
63- `Mutation[]`
64 - Mutation instances from the cache
65β
66## `mutationCache.subscribe`
67β
68The `subscribe` method can be used to subscribe to the mutation cache as a whole and be informed of safe/known updates to the cache like mutation states changing or mutations being updated, added or removed.
69β
70```tsx
71const callback = event => {
72 console.log(event.type, event.mutation)
73}
74β
75const unsubscribe = mutationCache.subscribe(callback)
76```
77β
78**Options**
79β
80- `callback: (mutation?: MutationCacheNotifyEvent) => void`
81 - This function will be called with the mutation cache any time it is updated.
82β
83**Returns**
84β
85- `unsubscribe: Function => void`
86 - This function will unsubscribe the callback from the mutation cache.
87β
88## `mutationCache.clear`
89β
90The `clear` method can be used to clear the cache entirely and start fresh.
91β
92```tsx
93mutationCache.clear()
94```