Skip to main content

useMutation

The useMutation hook is used to create, update, or delete data. Unlike queries, mutations are typically used to modify server-side data.

Import

Signature

Type Parameters

type
default:"unknown"
The type of data returned by the mutation function
type
default:"DefaultError"
The type of error that can be thrown by the mutation function
type
default:"void"
The type of variables object passed to the mutation function
type
default:"unknown"
The type of the context returned from the onMutate callback

Parameters

UseMutationOptions
required
Configuration options for the mutation
(variables: TVariables) => Promise<TData>
required
The function that performs the mutation. Receives the variables passed to mutate.
unknown[]
Optional mutation key for identifying and tracking the mutation
(variables: TVariables) => Promise<TOnMutateResult> | TOnMutateResult
This function will fire before the mutation function is fired and receives the same variables the mutation function receives. Useful for optimistic updates.
(data: TData, variables: TVariables, context: TOnMutateResult) => Promise<unknown> | unknown
This function will fire when the mutation is successful and receives the mutation’s result
(error: TError, variables: TVariables, context: TOnMutateResult | undefined) => Promise<unknown> | unknown
This function will fire if the mutation encounters an error
(data: TData | undefined, error: TError | null, variables: TVariables, context: TOnMutateResult | undefined) => Promise<unknown> | unknown
This function will fire when the mutation is either successful or encounters an error
boolean | number | (failureCount: number, error: TError) => boolean
default:"false"
Number of retry attempts or function to determine if a mutation should be retried
number | (retryAttempt: number, error: TError) => number
Function that receives a retry attempt number and returns the delay to apply before the next attempt
'online' | 'always' | 'offlineFirst'
default:"'online'"
Controls when the mutation function is allowed to execute
boolean | (error: TError) => boolean
default:"false"
Set this to true if you want errors to be thrown in the render phase
Record<string, unknown>
Optional metadata that can be used by mutation observers or plugins
number
default:"300000"
Time in milliseconds that unused/inactive cache data remains in memory
QueryClient
Optional QueryClient instance to use. If not provided, the client from the nearest QueryClientProvider will be used.

Returns

(variables: TVariables, options?: MutateOptions) => void
Function to trigger the mutation. This is a synchronous function that doesn’t return a promise.Variables:
  • variables: The variables to pass to the mutation function
Options:
  • onSuccess: Override or extend the mutation’s onSuccess callback
  • onError: Override or extend the mutation’s onError callback
  • onSettled: Override or extend the mutation’s onSettled callback
(variables: TVariables, options?: MutateOptions) => Promise<TData>
Async version of mutate that returns a promise. Useful when you need to await the result or handle it with promise chains.
TData | undefined
The data returned by the last successful mutation
TError | null
The error object for the mutation, if an error occurred
'idle' | 'pending' | 'error' | 'success'
The status of the mutation:
  • idle: The mutation is currently idle or in a fresh/reset state
  • pending: The mutation is currently running
  • error: The mutation encountered an error
  • success: The mutation was successful
boolean
Derived from status. Will be true if the mutation is currently executing
boolean
Derived from status. Will be true if the mutation is in success status
boolean
Derived from status. Will be true if the mutation is in error status
boolean
Derived from status. Will be true if the mutation is in idle status
() => void
Function to clean the mutation internal state (e.g., data, error, status, etc.)
TVariables | undefined
The variables passed to the mutation function
number
The failure count for the mutation
TError | null
The failure reason for the mutation retry
number
Timestamp of when the mutation was submitted

Examples

Basic Usage

With Type Safety

Optimistic Updates

With Async/Await

Invalidating Queries

Source

Implementation: useMutation.ts:19