* docs(query-core/utils): add missing JSDoc to utility functions * docs(query-core/utils): clarify that 'addToEnd' and 'addToStart' drop at most one item * docs(framework/*/reference): regenerate reference docs
5.7 KiB
| id | title | redirect_from | ||
|---|---|---|---|---|
| MutationCache | MutationCache |
|
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.
Example
const unsubscribe = mutationCache.subscribe((event) => {
console.log(event.type, event.mutation)
})
Extends
Subscribable<MutationCacheListener>
Constructors
Constructor
new MutationCache(config: MutationCacheConfig): MutationCache;
Defined in: packages/query-core/src/mutationCache.ts:128
Parameters
config
MutationCacheConfig = {}
Returns
MutationCache
Overrides
Subscribable<MutationCacheListener>.constructor
Properties
config
config: MutationCacheConfig = {};
Defined in: packages/query-core/src/mutationCache.ts:128
Methods
clear()
clear(): void;
Defined in: packages/query-core/src/mutationCache.ts:234
Removes all mutations from the cache.
Returns
void
Example
const mutationCache = queryClient.getMutationCache()
mutationCache.clear()
find()
find<TData, TError, TVariables, TOnMutateResult>(filters: MutationFilters):
| Mutation<TData, TError, TVariables, TOnMutateResult>
| undefined;
Defined in: packages/query-core/src/mutationCache.ts:274
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.
Type Parameters
TData
TData = unknown
TError
TError = Error
TVariables
TVariables = any
TOnMutateResult
TOnMutateResult = unknown
Parameters
filters
Returns
| Mutation<TData, TError, TVariables, TOnMutateResult>
| undefined
See
Example
const mutationCache = queryClient.getMutationCache()
const mutation = mutationCache.find({ mutationKey: ['addPost'] })
findAll()
findAll(filters: MutationFilters): Mutation<unknown, Error, unknown, unknown>[];
Defined in: packages/query-core/src/mutationCache.ts:303
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.
Parameters
filters
MutationFilters = {}
Returns
Mutation<unknown, Error, unknown, unknown>[]
See
Example
const mutationCache = queryClient.getMutationCache()
const mutations = mutationCache.findAll({ mutationKey: ['addPost'] })
getAll()
getAll(): Mutation<unknown, Error, unknown, unknown>[];
Defined in: packages/query-core/src/mutationCache.ts:256
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.
Returns
Mutation<unknown, Error, unknown, unknown>[]
Example
const mutationCache = queryClient.getMutationCache()
const mutations = mutationCache.getAll()
hasListeners()
hasListeners(): boolean;
Defined in: packages/query-core/src/subscribable.ts:41
Returns true while at least one listener is registered, false once they have all unsubscribed.
Returns
boolean
Inherited from
Subscribable.hasListeners
subscribe()
subscribe(listener: MutationCacheListener): () => void;
Defined in: packages/query-core/src/subscribable.ts:27
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().
Parameters
listener
MutationCacheListener
Called on each update, with whatever the subclass passes to its subscribers.
Returns
(): void;
Returns
void
Example
const unsubscribe = subscribable.subscribe(() => {
// react to the update
})
unsubscribe()
Inherited from
Subscribable.subscribe