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

MutationCache.md

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