Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

MutationCache.md

337 lines6.6 KB

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```