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