Open History to see the last commit
1---
2id: MutationCache
3title: MutationCache
4redirect_from:
5 - reference/MutationCache
6 - framework/react/reference/MutationCache
7---
8β
9Defined in: [packages/query-core/src/mutationCache.ts:124](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L124)
10β
11The `MutationCache` is the storage for mutations.
12β
13Normally, you will not interact with the `MutationCache` directly and instead use a
14`QueryClient`. You can subscribe to it (inherited from `Subscribable`) to be informed of
15safe/known updates to the cache, such as mutations being added, removed, or updated.
16β
17## Example
18β
19```ts
20const unsubscribe = mutationCache.subscribe((event) => {
21 console.log(event.type, event.mutation)
22})
23```
24β
25## Extends
26β
27- `Subscribable`\<`MutationCacheListener`\>
28β
29## Constructors
30β
31### Constructor
32β
33```ts
34new MutationCache(config: MutationCacheConfig): MutationCache;
35```
36β
37Defined in: [packages/query-core/src/mutationCache.ts:129](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L129)
38β
39#### Parameters
40β
41##### config
42β
43[`MutationCacheConfig`](../interfaces/MutationCacheConfig.md) = `{}`
44β
45#### Returns
46β
47`MutationCache`
48β
49#### Overrides
50β
51```ts
52Subscribable<MutationCacheListener>.constructor
53```
54β
55## Properties
56β
57### config
58β
59```ts
60config: MutationCacheConfig = {};
61```
62β
63Defined in: [packages/query-core/src/mutationCache.ts:129](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L129)
64β
65## Methods
66β
67### clear()
68β
69```ts
70clear(): void;
71```
72β
73Defined in: [packages/query-core/src/mutationCache.ts:236](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L236)
74β
75Removes all mutations from the cache.
76β
77#### Returns
78β
79`void`
80β
81#### Example
82β
83```ts
84const mutationCache = queryClient.getMutationCache()
85β
86mutationCache.clear()
87```
88β
89***
90β
91### find()
92β
93```ts
94find<TData, TError, TVariables, TOnMutateResult>(filters: MutationFilters):
95 | Mutation<TData, TError, TVariables, TOnMutateResult>
96 | undefined;
97```
98β
99Defined in: [packages/query-core/src/mutationCache.ts:278](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L278)
100β
101A slightly more advanced method that can be used to get an existing mutation instance from
102the cache. If the mutation does not exist, `undefined` is returned.
103β
104This is not typically needed for most applications, but can come in handy when needing more
105information about a mutation in rare scenarios.
106β
107#### Type Parameters
108β
109##### TData
110β
111`TData` = `unknown`
112β
113##### TError
114β
115`TError` = `Error`
116β
117##### TVariables
118β
119`TVariables` = `any`
120β
121##### TOnMutateResult
122β
123`TOnMutateResult` = `unknown`
124β
125#### Parameters
126β
127##### filters
128β
129[`MutationFilters`](../interfaces/MutationFilters.md)
130β
131#### Returns
132β
133 \| [`Mutation`](Mutation.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
134 \| `undefined`
135β
136#### See
137β
138[MutationCache#findAll](#findall)
139β
140#### Example
141β
142```ts
143const mutationCache = queryClient.getMutationCache()
144β
145const mutation = mutationCache.find({ mutationKey: ['addPost'] })
146```
147β
148***
149β
150### findAll()
151β
152```ts
153findAll(filters: MutationFilters): Mutation<unknown, Error, unknown, unknown>[];
154```
155β
156Defined in: [packages/query-core/src/mutationCache.ts:308](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L308)
157β
158An even more advanced method that can be used to get existing mutation instances from the
159cache that match the given filters. If no mutations match, an empty array is returned.
160β
161This is not typically needed for most applications, but can come in handy when needing more
162information about mutations in rare scenarios.
163β
164#### Parameters
165β
166##### filters
167β
168[`MutationFilters`](../interfaces/MutationFilters.md) = `{}`
169β
170#### Returns
171β
172[`Mutation`](Mutation.md)\<`unknown`, `Error`, `unknown`, `unknown`\>[]
173β
174#### See
175β
176[MutationCache#find](#find)
177β
178#### Example
179β
180```ts
181const mutationCache = queryClient.getMutationCache()
182β
183const mutations = mutationCache.findAll({ mutationKey: ['addPost'] })
184```
185β
186***
187β
188### getAll()
189β
190```ts
191getAll(): Mutation<unknown, Error, unknown, unknown>[];
192```
193β
194Defined in: [packages/query-core/src/mutationCache.ts:259](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L259)
195β
196Returns all mutations within the cache.
197β
198This is not typically needed for most applications, but can come in handy when needing more
199information about a mutation in rare scenarios.
200β
201#### Returns
202β
203[`Mutation`](Mutation.md)\<`unknown`, `Error`, `unknown`, `unknown`\>[]
204β
205#### Example
206β
207```ts
208const mutationCache = queryClient.getMutationCache()
209β
210const mutations = mutationCache.getAll()
211```
212β
213***
214β
215### hasListeners()
216β
217```ts
218hasListeners(): boolean;
219```
220β
221Defined in: [packages/query-core/src/subscribable.ts:41](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L41)
222β
223Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
224β
225#### Returns
226β
227`boolean`
228β
229#### Inherited from
230β
231```ts
232Subscribable.hasListeners
233```
234β
235***
236β
237### subscribe()
238β
239```ts
240subscribe(listener: MutationCacheListener): () => void;
241```
242β
243Defined in: [packages/query-core/src/subscribable.ts:27](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L27)
244β
245Registers a listener to be called on every update this object notifies about. Returns a function
246that removes the listener again β call it to stop listening. The base class never drops a listener
247on its own, though some subclasses clear all of theirs in `destroy()`.
248β
249#### Parameters
250β
251##### listener
252β
253`MutationCacheListener`
254β
255Called on each update, with whatever the subclass passes to its subscribers.
256β
257#### Returns
258β
259```ts
260(): void;
261```
262β
263##### Returns
264β
265`void`
266β
267#### Example
268β
269```ts
270const unsubscribe = subscribable.subscribe(() => {
271 // react to the update
272})
273β
274unsubscribe()
275```
276β
277#### Inherited from
278β
279```ts
280Subscribable.subscribe
281```