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

MutationCache.md

289 lines6.0 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: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```