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***
66β
67### listeners
68β
69```ts
70protected listeners: Set<MutationCacheListener>;
71```
72β
73Defined in: [packages/query-core/src/subscribable.ts:7](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L7)
74β
75#### Inherited from
76β
77```ts
78Subscribable.listeners
79```
80β
81## Methods
82β
83### clear()
84β
85```ts
86clear(): void;
87```
88β
89Defined in: [packages/query-core/src/mutationCache.ts:236](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L236)
90β
91Removes all mutations from the cache.
92β
93#### Returns
94β
95`void`
96β
97#### Example
98β
99```ts
100const mutationCache = queryClient.getMutationCache()
101β
102mutationCache.clear()
103```
104β
105***
106β
107### find()
108β
109```ts
110find<TData, TError, TVariables, TOnMutateResult>(filters: MutationFilters):
111 | Mutation<TData, TError, TVariables, TOnMutateResult>
112 | undefined;
113```
114β
115Defined in: [packages/query-core/src/mutationCache.ts:278](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L278)
116β
117A slightly more advanced method that can be used to get an existing mutation instance from
118the cache. If the mutation does not exist, `undefined` is returned.
119β
120This is not typically needed for most applications, but can come in handy when needing more
121information about a mutation in rare scenarios.
122β
123#### Type Parameters
124β
125##### TData
126β
127`TData` = `unknown`
128β
129##### TError
130β
131`TError` = `Error`
132β
133##### TVariables
134β
135`TVariables` = `any`
136β
137##### TOnMutateResult
138β
139`TOnMutateResult` = `unknown`
140β
141#### Parameters
142β
143##### filters
144β
145[`MutationFilters`](../interfaces/MutationFilters.md)
146β
147#### Returns
148β
149 \| [`Mutation`](Mutation.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
150 \| `undefined`
151β
152#### See
153β
154[MutationCache#findAll](#findall)
155β
156#### Example
157β
158```ts
159const mutationCache = queryClient.getMutationCache()
160β
161const mutation = mutationCache.find({ mutationKey: ['addPost'] })
162```
163β
164***
165β
166### findAll()
167β
168```ts
169findAll(filters: MutationFilters): Mutation<unknown, Error, unknown, unknown>[];
170```
171β
172Defined in: [packages/query-core/src/mutationCache.ts:308](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L308)
173β
174An even more advanced method that can be used to get existing mutation instances from the
175cache that match the given filters. If no mutations match, an empty array is returned.
176β
177This is not typically needed for most applications, but can come in handy when needing more
178information about mutations in rare scenarios.
179β
180#### Parameters
181β
182##### filters
183β
184[`MutationFilters`](../interfaces/MutationFilters.md) = `{}`
185β
186#### Returns
187β
188[`Mutation`](Mutation.md)\<`unknown`, `Error`, `unknown`, `unknown`\>[]
189β
190#### See
191β
192[MutationCache#find](#find)
193β
194#### Example
195β
196```ts
197const mutationCache = queryClient.getMutationCache()
198β
199const mutations = mutationCache.findAll({ mutationKey: ['addPost'] })
200```
201β
202***
203β
204### getAll()
205β
206```ts
207getAll(): Mutation<unknown, Error, unknown, unknown>[];
208```
209β
210Defined in: [packages/query-core/src/mutationCache.ts:259](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L259)
211β
212Returns all mutations within the cache.
213β
214This is not typically needed for most applications, but can come in handy when needing more
215information about a mutation in rare scenarios.
216β
217#### Returns
218β
219[`Mutation`](Mutation.md)\<`unknown`, `Error`, `unknown`, `unknown`\>[]
220β
221#### Example
222β
223```ts
224const mutationCache = queryClient.getMutationCache()
225β
226const mutations = mutationCache.getAll()
227```
228β
229***
230β
231### hasListeners()
232β
233```ts
234hasListeners(): boolean;
235```
236β
237Defined in: [packages/query-core/src/subscribable.ts:41](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L41)
238β
239Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
240β
241#### Returns
242β
243`boolean`
244β
245#### Inherited from
246β
247```ts
248Subscribable.hasListeners
249```
250β
251***
252β
253### onSubscribe()
254β
255```ts
256protected onSubscribe(): void;
257```
258β
259Defined in: [packages/query-core/src/subscribable.ts:45](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L45)
260β
261#### Returns
262β
263`void`
264β
265#### Inherited from
266β
267```ts
268Subscribable.onSubscribe
269```
270β
271***
272β
273### onUnsubscribe()
274β
275```ts
276protected onUnsubscribe(): void;
277```
278β
279Defined in: [packages/query-core/src/subscribable.ts:49](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L49)
280β
281#### Returns
282β
283`void`
284β
285#### Inherited from
286β
287```ts
288Subscribable.onUnsubscribe
289```
290β
291***
292β
293### subscribe()
294β
295```ts
296subscribe(listener: MutationCacheListener): () => void;
297```
298β
299Defined in: [packages/query-core/src/subscribable.ts:27](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L27)
300β
301Registers a listener to be called on every update this object notifies about. Returns a function
302that removes the listener again β call it to stop listening. The base class never drops a listener
303on its own, though some subclasses clear all of theirs in `destroy()`.
304β
305#### Parameters
306β
307##### listener
308β
309`MutationCacheListener`
310β
311Called on each update, with whatever the subclass passes to its subscribers.
312β
313#### Returns
314β
315```ts
316(): void;
317```
318β
319##### Returns
320β
321`void`
322β
323#### Example
324β
325```ts
326const unsubscribe = subscribable.subscribe(() => {
327 // react to the update
328})
329β
330unsubscribe()
331```
332β
333#### Inherited from
334β
335```ts
336Subscribable.subscribe
337```