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

MutationCache.md

278 lines5.7 KB

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