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```js
11import { MutationCache } from 'react-query'
12β
13const mutationCache = new MutationCache({
14 onError: error => {
15 console.log(error)
16 },
17})
18```
19β
20Its available methods are:
21β
22- [`getAll`](#mutationcachegetall)
23- [`subscribe`](#mutationcachesubscribe)
24- [`clear`](#mutationcacheclear)
25β
26**Options**
27β
28- `onError?: (error: unknown, variables: unknown, context: unknown, mutation: Mutation) => void`
29 - Optional
30 - This function will be called if some mutation encounters an error.
31β
32## `mutationCache.getAll`
33β
34`getAll` returns all mutations within the cache.
35β
36> Note: This is not typically needed for most applications, but can come in handy when needing more information about a mutation in rare scenarios
37β
38```js
39const mutations = mutationCache.getAll()
40```
41β
42**Returns**
43β
44- `Mutation[]`
45 - Mutation instances from the cache
46β
47## `mutationCache.subscribe`
48β
49The `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.
50β
51```js
52const callback = mutation => {
53 console.log(mutation)
54}
55β
56const unsubscribe = mutationCache.subscribe(callback)
57```
58β
59**Options**
60β
61- `callback: (mutation?: Mutation) => void`
62 - This function will be called with the mutation cache any time it is updated.
63β
64**Returns**
65β
66- `unsubscribe: Function => void`
67 - This function will unsubscribe the callback from the mutation cache.
68β
69## `mutationCache.clear`
70β
71The `clear` method can be used to clear the cache entirely and start fresh.
72β
73```js
74mutationCache.clear()
75```