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
The type of data returned by the mutation function
The type of error that can be thrown by the mutation function
The type of variables object passed to the mutation function
The type of the context returned from the onMutate callback
Parameters
Configuration options for the mutation
The function that performs the mutation. Receives the variables passed to
mutate.Optional mutation key for identifying and tracking the mutation
This function will fire before the mutation function is fired and receives the same variables the mutation function receives. Useful for optimistic updates.
onSuccess
(data: TData, variables: TVariables, context: TOnMutateResult) => Promise<unknown> | unknown
This function will fire when the mutation is successful and receives the mutation’s result
onError
(error: TError, variables: TVariables, context: TOnMutateResult | undefined) => Promise<unknown> | unknown
This function will fire if the mutation encounters an error
onSettled
(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
Number of retry attempts or function to determine if a mutation should be retried
Function that receives a retry attempt number and returns the delay to apply before the next attempt
Controls when the mutation function is allowed to execute
Set this to
true if you want errors to be thrown in the render phaseOptional metadata that can be used by mutation observers or plugins
Time in milliseconds that unused/inactive cache data remains in memory
Optional QueryClient instance to use. If not provided, the client from the nearest
QueryClientProvider will be used.Returns
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
Async version of
mutate that returns a promise. Useful when you need to await the result or handle it with promise chains.The data returned by the last successful mutation
The error object for the mutation, if an error occurred
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
Derived from
status. Will be true if the mutation is currently executingDerived from
status. Will be true if the mutation is in success statusDerived from
status. Will be true if the mutation is in error statusDerived from
status. Will be true if the mutation is in idle statusFunction to clean the mutation internal state (e.g., data, error, status, etc.)
The variables passed to the mutation function
The failure count for the mutation
The failure reason for the mutation retry
Timestamp of when the mutation was submitted