Open History to see the last commit
1---
2id: MutationCache
3title: MutationCache
4---
5β
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7β
8Defined in: [packages/query-core/src/mutationCache.ts:123](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L123)
9β
10The `MutationCache` is the storage for mutations.
11β
12Normally, you will not interact with the `MutationCache` directly and instead use a
13`QueryClient`. You can subscribe to it (inherited from `Subscribable`) to be informed of
14safe/known updates to the cache, such as mutations being added, removed, or updated.
15β
16## Example
17β
18```ts
19const unsubscribe = mutationCache.subscribe((event) => {
20 console.log(event.type, event.mutation)
21})
22```
23β
24## Extends
25β
26- `Subscribable`\<`MutationCacheListener`\>
27β
28## Constructors
29β
30### Constructor
31β
32```ts
33new MutationCache(config?: MutationCacheConfig): MutationCache;
34```
35β
36Defined in: [packages/query-core/src/mutationCache.ts:128](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L128)
37β
38#### Parameters
39β
40##### config?
41β
42[`MutationCacheConfig`](../interfaces/MutationCacheConfig.md) = `{}`
43β
44#### Returns
45β
46`MutationCache`
47β
48#### Overrides
49β
50```ts
51Subscribable<MutationCacheListener>.constructor
52```
53β
54## Properties
55β
56### config
57β
58```ts
59config: MutationCacheConfig = {};
60```
61β
62Defined in: [packages/query-core/src/mutationCache.ts:128](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L128)
63β
64## Methods
65β
66### clear()
67β
68```ts
69clear(): void;
70```
71β
72Defined in: [packages/query-core/src/mutationCache.ts:262](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L262)
73β
74Removes all mutations from the cache.
75β
76#### Returns
77β
78`void`
79β
80#### Example
81β
82```ts
83const mutationCache = queryClient.getMutationCache()
84β
85mutationCache.clear()
86```
87β
88***
89β
90### find()
91β
92```ts
93find<TData = unknown, TError = Error, TVariables = any, TOnMutateResult = unknown>(filters: MutationFilters):
94 | Mutation<TData, TError, TVariables, TOnMutateResult>
95 | undefined;
96```
97β
98Defined in: [packages/query-core/src/mutationCache.ts:306](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L306)
99β
100A slightly more advanced method that can be used to get an existing mutation instance from
101the cache. If the mutation does not exist, `undefined` is returned.
102β
103This is not typically needed for most applications, but can come in handy when needing more
104information about a mutation in rare scenarios.
105β
106#### Type Parameters
107β
108##### TData
109β
110`TData` = `unknown`
111β
112##### TError
113β
114`TError` = `Error`
115β
116##### TVariables
117β
118`TVariables` = `any`
119β
120##### TOnMutateResult
121β
122`TOnMutateResult` = `unknown`
123β
124#### Parameters
125β
126##### filters
127β
128[`MutationFilters`](../interfaces/MutationFilters.md)
129β
130The filters to match. `exact` defaults to `true`.
131β
132#### Returns
133β
134 \| [`Mutation`](Mutation.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
135 \| `undefined`
136β
137The first matching mutation, or `undefined`.
138β
139#### See
140β
141[MutationCache#findAll](#findall)
142β
143#### Example
144β
145```ts
146const mutationCache = queryClient.getMutationCache()
147β
148const mutation = mutationCache.find({ mutationKey: ['addPost'] })
149```
150β
151***
152β
153### findAll()
154β
155```ts
156findAll(filters?: MutationFilters): Mutation<unknown, Error, unknown, unknown>[];
157```
158β
159Defined in: [packages/query-core/src/mutationCache.ts:337](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L337)
160β
161An even more advanced method that can be used to get existing mutation instances from the
162cache that match the given filters. If no mutations match, an empty array is returned.
163β
164This is not typically needed for most applications, but can come in handy when needing more
165information about mutations in rare scenarios.
166β
167#### Parameters
168β
169##### filters?
170β
171[`MutationFilters`](../interfaces/MutationFilters.md) = `{}`
172β
173The filters to match. Without filters, every mutation is returned.
174β
175#### Returns
176β
177[`Mutation`](Mutation.md)\<`unknown`, `Error`, `unknown`, `unknown`\>[]
178β
179The matching mutations.
180β
181#### See
182β
183[MutationCache#find](#find)
184β
185#### Example
186β
187```ts
188const mutationCache = queryClient.getMutationCache()
189β
190const mutations = mutationCache.findAll({ mutationKey: ['addPost'] })
191```
192β
193***
194β
195### getAll()
196β
197```ts
198getAll(): Mutation<unknown, Error, unknown, unknown>[];
199```
200β
201Defined in: [packages/query-core/src/mutationCache.ts:286](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L286)
202β
203Returns all mutations within the cache.
204β
205This is not typically needed for most applications, but can come in handy when needing more
206information about a mutation in rare scenarios.
207β
208#### Returns
209β
210[`Mutation`](Mutation.md)\<`unknown`, `Error`, `unknown`, `unknown`\>[]
211β
212Every mutation in the cache.
213β
214#### Example
215β
216```ts
217const mutationCache = queryClient.getMutationCache()
218β
219const mutations = mutationCache.getAll()
220```
221β
222***
223β
224### hasListeners()
225β
226```ts
227hasListeners(): boolean;
228```
229β
230Defined in: [packages/query-core/src/subscribable.ts:43](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L43)
231β
232Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
233β
234#### Returns
235β
236`boolean`
237β
238`true` if at least one listener is registered.
239β
240#### Inherited from
241β
242```ts
243Subscribable.hasListeners
244```
245β
246***
247β
248### subscribe()
249β
250```ts
251subscribe(listener: MutationCacheListener): () => void;
252```
253β
254Defined in: [packages/query-core/src/subscribable.ts:28](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L28)
255β
256Registers a listener to be called on every update this object notifies about. Returns a function
257that removes the listener again β call it to stop listening. The base class never drops a listener
258on its own, though some subclasses clear all of theirs in `destroy()`.
259β
260#### Parameters
261β
262##### listener
263β
264`MutationCacheListener`
265β
266Called on each update, with whatever the subclass passes to its subscribers.
267β
268#### Returns
269β
270A function that removes the listener.
271β
272() => `void`
273β
274#### Example
275β
276```ts
277const unsubscribe = subscribable.subscribe(() => {
278 // react to the update
279})
280β
281unsubscribe()
282```
283β
284#### Inherited from
285β
286```ts
287Subscribable.subscribe
288```