Application-wide mutation status
Show a global saving or loading indicator whenever any mutation is pending.
TanStack · Hook
useIsMutating returns the number of currently pending mutations. Optional filters can limit the count, making it useful for application-wide loading indicators.
Call this hook to subscribe to the number of currently pending mutations. Pass mutation filters to narrow which mutations count, and optionally provide a custom QueryClient; otherwise the nearest context supplies it.
function useIsMutating(filters?: MutationFilters<unknown, Error, unknown, unknown>, queryClient?: QueryClient): number;| Name | Type | Description |
|---|---|---|
filters | MutationFilters<unknown, Error, unknown, unknown> | Optional filters used to narrow the mutations included in the count; supported fields include exact, mutationKey, predicate, and status. |
queryClient | QueryClient | Optional client used to inspect mutations. If omitted, the nearest context provides it. |
number — the number of mutations currently pending.
import { useIsMutating } from '@tanstack/react-query'
export function PostsMutatingIndicator() {
const count = useIsMutating({ mutationKey: ['posts'] })
return count > 0 ? <span>Saving posts...</span> : null
}Counts pending mutations whose keys match the posts prefix and displays an indicator while at least one is active.
Show a global saving or loading indicator whenever any mutation is pending.
Display progress feedback only while mutations for a feature key are active.
Observe mutations from a separately configured QueryClient.
It returns a number representing currently pending mutations, making it suitable for an app-wide loading indicator. With no filters, the count covers all pending mutations.
Follow-up: How would you count only post mutations? Pass a filter such as { mutationKey: ['posts'] } and use the returned number to control the indicator.
The first argument is an optional MutationFilters object. It can narrow matches by mutation key, exactness, predicate, or status.
Follow-up: When would exact be useful? Use exact when a key should match only that precise mutation key rather than mutations beneath its key prefix.
The optional second argument is a QueryClient. When omitted, the hook uses the QueryClient from the nearest context.
Follow-up: Why pass a client explicitly? This lets the hook observe mutations managed by a particular custom client instead of the contextual default.
useIsMutatinguseMutationuseMutationStateQueryClientMutationFilters