useMutation
TheuseMutation 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 phaseRecord<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
onSuccess: Override or extend the mutation’s onSuccess callbackonError: Override or extend the mutation’s onError callbackonSettled: 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 statepending: The mutation is currently runningerror: The mutation encountered an errorsuccess: The mutation was successful
boolean
Derived from
status. Will be true if the mutation is currently executingboolean
Derived from
status. Will be true if the mutation is in success statusboolean
Derived from
status. Will be true if the mutation is in error statusboolean
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