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