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

MutationCache.md

328 lines5.2 KB

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