Defined in: packages/query-core/src/mutationCache.ts:123
The MutationCache is the storage for mutations.
Normally, you will not interact with the MutationCache directly and instead use a QueryClient. You can subscribe to it (inherited from Subscribable) to be informed of safe/known updates to the cache, such as mutations being added, removed, or updated.
const unsubscribe = mutationCache.subscribe((event) => {
console.log(event.type, event.mutation)
})new MutationCache(config: MutationCacheConfig): MutationCache;Defined in: packages/query-core/src/mutationCache.ts:128
MutationCacheConfig = {}
MutationCache
Subscribable<MutationCacheListener>.constructorconfig: MutationCacheConfig = {};Defined in: packages/query-core/src/mutationCache.ts:128
clear(): void;Defined in: packages/query-core/src/mutationCache.ts:262
Removes all mutations from the cache.
void
const mutationCache = queryClient.getMutationCache()
mutationCache.clear()find<TData, TError, TVariables, TOnMutateResult>(filters: MutationFilters):
| Mutation<TData, TError, TVariables, TOnMutateResult>
| undefined;Defined in: packages/query-core/src/mutationCache.ts:305
A slightly more advanced method that can be used to get an existing mutation instance from the cache. If the mutation does not exist, undefined is returned.
This is not typically needed for most applications, but can come in handy when needing more information about a mutation in rare scenarios.
TData = unknown
TError = Error
TVariables = any
TOnMutateResult = unknown
The filters to match. exact defaults to true.
| Mutation<TData, TError, TVariables, TOnMutateResult> | undefined
The first matching mutation, or undefined.
const mutationCache = queryClient.getMutationCache()
const mutation = mutationCache.find({ mutationKey: ['addPost'] })findAll(filters: MutationFilters): Mutation<unknown, Error, unknown, unknown>[];Defined in: packages/query-core/src/mutationCache.ts:336
An even more advanced method that can be used to get existing mutation instances from the cache that match the given filters. If no mutations match, an empty array is returned.
This is not typically needed for most applications, but can come in handy when needing more information about mutations in rare scenarios.
MutationFilters = {}
The filters to match. Without filters, every mutation is returned.
Mutation<unknown, Error, unknown, unknown>[]
The matching mutations.
const mutationCache = queryClient.getMutationCache()
const mutations = mutationCache.findAll({ mutationKey: ['addPost'] })getAll(): Mutation<unknown, Error, unknown, unknown>[];Defined in: packages/query-core/src/mutationCache.ts:285
Returns all mutations within the cache.
This is not typically needed for most applications, but can come in handy when needing more information about a mutation in rare scenarios.
Mutation<unknown, Error, unknown, unknown>[]
Every mutation in the cache.
const mutationCache = queryClient.getMutationCache()
const mutations = mutationCache.getAll()hasListeners(): boolean;Defined in: packages/query-core/src/subscribable.ts:43
Returns true while at least one listener is registered, false once they have all unsubscribed.
boolean
true if at least one listener is registered.
Subscribable.hasListenerssubscribe(listener: MutationCacheListener): () => void;Defined in: packages/query-core/src/subscribable.ts:28
Registers a listener to be called on every update this object notifies about. Returns a function that removes the listener again — call it to stop listening. The base class never drops a listener on its own, though some subclasses clear all of theirs in destroy().
MutationCacheListener
Called on each update, with whatever the subclass passes to its subscribers.
A function that removes the listener.
(): void;void
const unsubscribe = subscribable.subscribe(() => {
// react to the update
})
unsubscribe()Subscribable.subscribe