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:123](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L123)
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:128](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L128)
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:128](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L128)
64β
65## Methods
66β
67### clear()
68β
69```ts
70clear(): void;
71```
72β
73Defined in: [packages/query-core/src/mutationCache.ts:262](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L262)
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:305](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L305)
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β
131The filters to match. `exact` defaults to `true`.
132β
133#### Returns
134β
135 \| [`Mutation`](Mutation.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
136 \| `undefined`
137β
138The first matching mutation, or `undefined`.
139β
140#### See
141β
142[MutationCache#findAll](#findall)
143β
144#### Example
145β
146```ts
147const mutationCache = queryClient.getMutationCache()
148β
149const mutation = mutationCache.find({ mutationKey: ['addPost'] })
150```
151β
152***
153β
154### findAll()
155β
156```ts
157findAll(filters?: MutationFilters): Mutation<unknown, Error, unknown, unknown>[];
158```
159β
160Defined in: [packages/query-core/src/mutationCache.ts:336](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L336)
161β
162An even more advanced method that can be used to get existing mutation instances from the
163cache that match the given filters. If no mutations match, an empty array is returned.
164β
165This is not typically needed for most applications, but can come in handy when needing more
166information about mutations in rare scenarios.
167β
168#### Parameters
169β
170##### filters?
171β
172[`MutationFilters`](../interfaces/MutationFilters.md) = `{}`
173β
174The filters to match. Without filters, every mutation is returned.
175β
176#### Returns
177β
178[`Mutation`](Mutation.md)\<`unknown`, `Error`, `unknown`, `unknown`\>[]
179β
180The matching mutations.
181β
182#### See
183β
184[MutationCache#find](#find)
185β
186#### Example
187β
188```ts
189const mutationCache = queryClient.getMutationCache()
190β
191const mutations = mutationCache.findAll({ mutationKey: ['addPost'] })
192```
193β
194***
195β
196### getAll()
197β
198```ts
199getAll(): Mutation<unknown, Error, unknown, unknown>[];
200```
201β
202Defined in: [packages/query-core/src/mutationCache.ts:285](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L285)
203β
204Returns all mutations within the cache.
205β
206This is not typically needed for most applications, but can come in handy when needing more
207information about a mutation in rare scenarios.
208β
209#### Returns
210β
211[`Mutation`](Mutation.md)\<`unknown`, `Error`, `unknown`, `unknown`\>[]
212β
213Every mutation in the cache.
214β
215#### Example
216β
217```ts
218const mutationCache = queryClient.getMutationCache()
219β
220const mutations = mutationCache.getAll()
221```
222β
223***
224β
225### hasListeners()
226β
227```ts
228hasListeners(): boolean;
229```
230β
231Defined in: [packages/query-core/src/subscribable.ts:43](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L43)
232β
233Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
234β
235#### Returns
236β
237`boolean`
238β
239`true` if at least one listener is registered.
240β
241#### Inherited from
242β
243```ts
244Subscribable.hasListeners
245```
246β
247***
248β
249### subscribe()
250β
251```ts
252subscribe(listener: MutationCacheListener): () => void;
253```
254β
255Defined in: [packages/query-core/src/subscribable.ts:28](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L28)
256β
257Registers a listener to be called on every update this object notifies about. Returns a function
258that removes the listener again β call it to stop listening. The base class never drops a listener
259on its own, though some subclasses clear all of theirs in `destroy()`.
260β
261#### Parameters
262β
263##### listener
264β
265`MutationCacheListener`
266β
267Called on each update, with whatever the subclass passes to its subscribers.
268β
269#### Returns
270β
271A function that removes the listener.
272β
273() => `void`
274β
275#### Example
276β
277```ts
278const unsubscribe = subscribable.subscribe(() => {
279 // react to the update
280})
281β
282unsubscribe()
283```
284β
285#### Inherited from
286β
287```ts
288Subscribable.subscribe
289```