Overview
The MutationObserver class is used to subscribe to a mutation and receive reactive updates when the mutation’s state changes. It’s the foundation for framework-specific hooks like useMutation in React.
Constructor
options MutationObserverOptions
required
Options for the mutation observer mutationFn (variables: TVariables, context: MutationFunctionContext) => Promise<TData>
The function to execute the mutation
Unique key for the mutation
onMutate (variables: TVariables, context: MutationFunctionContext) => Promise<TOnMutateResult> | TOnMutateResult
Callback fired before the mutation function. Return value is passed to other callbacks.
onSuccess (data: TData, variables: TVariables, onMutateResult: TOnMutateResult, context: MutationFunctionContext) => Promise<unknown> | unknown
Callback fired when the mutation succeeds
onError (error: TError, variables: TVariables, onMutateResult: TOnMutateResult | undefined, context: MutationFunctionContext) => Promise<unknown> | unknown
Callback fired when the mutation errors
onSettled (data: TData | undefined, error: TError | null, variables: TVariables, onMutateResult: TOnMutateResult | undefined, context: MutationFunctionContext) => Promise<unknown> | unknown
Callback fired when the mutation completes (success or error)
retry boolean | number | ((failureCount: number, error: TError) => boolean)
Retry failed mutations
retryDelay number | ((failureCount: number, error: TError) => number)
Delay between retry attempts in milliseconds
networkMode 'online' | 'always' | 'offlineFirst'
Network mode for the mutation. Defaults to 'online'.
Time in milliseconds that unused mutation results remain in memory
Scope for sequential mutation execution
Example:
Methods
subscribe()
Subscribes to mutation updates.
listener (result: MutationObserverResult<TData, TError, TVariables, TOnMutateResult>) => void
required
Callback function that receives mutation results
Returns: Function to unsubscribe
Example:
getCurrentResult()
Returns the current result without subscribing.
Returns: Current mutation result
The data returned from the mutation
The error object if mutation failed
The variables passed to the mutation function
context TOnMutateResult | undefined
The value returned from onMutate
status 'idle' | 'pending' | 'success' | 'error'
The status of the mutation
Is true when the mutation is currently executing
Is true when the mutation has succeeded
Is true when the mutation has failed
Is true when the mutation is in its initial state
mutate (variables: TVariables, options?: MutateOptions) => Promise<TData>
Function to trigger the mutation
Function to reset the mutation to its initial state
setOptions()
Updates the observer’s options.
options MutationObserverOptions
required
New options for the observer
Example:
mutate()
Executes the mutation.
Variables to pass to the mutation function
Additional options for this specific mutation call onSuccess (data: TData, variables: TVariables, onMutateResult: TOnMutateResult | undefined, context: MutationFunctionContext) => void
Callback for this specific mutation success
onError (error: TError, variables: TVariables, onMutateResult: TOnMutateResult | undefined, context: MutationFunctionContext) => void
Callback for this specific mutation error
onSettled (data: TData | undefined, error: TError | null, variables: TVariables, onMutateResult: TOnMutateResult | undefined, context: MutationFunctionContext) => void
Callback for this specific mutation completion
Returns: Promise that resolves with the mutation data or rejects with the error
Example:
reset()
Resets the mutation to its initial idle state.
Example:
Resetting removes the observer from the current mutation. A new call to mutate() will create a new mutation instance.
Properties
options
The current options for the observer.
Type Parameters
TData - The type of data returned by the mutation function
TError - The type of error that can be thrown (defaults to DefaultError)
TVariables - The type of variables passed to the mutation function (defaults to void)
TOnMutateResult - The type of value returned by the onMutate callback
Usage Example
Optimistic Updates
The MutationObserver is commonly used for optimistic updates: