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