const {
data,
error,
isError,
isIdle,
isPending,
isPaused,
isSuccess,
failureCount,
failureReason,
mutate,
mutateAsync,
reset,
status,
submittedAt,
variables,
} = useMutation(
{
mutationFn,
gcTime,
meta,
mutationKey,
networkMode,
onError,
onMutate,
onSettled,
onSuccess,
retry,
retryDelay,
scope,
throwOnError,
},
queryClient,
)
mutate(variables, {
onError,
onSettled,
onSuccess,
})
Parameter1 (Options)
mutationFn: (variables: TVariables, context: MutationFunctionContext) => Promise<TData>
variables is an object that mutate will pass to your mutationFncontext is an object that mutate will pass to your mutationFn. Contains reference to QueryClient, mutationKey and optional meta object.gcTime: number | Infinity
Infinity, will disable garbage collectionmutationKey: unknown[]
queryClient.setMutationDefaults.networkMode: 'online' | 'always' | 'offlineFirst'
'online'onMutate: (variables: TVariables, context: MutationFunctionContext) => Promise<TOnMutateResult | void> | TOnMutateResult | void
onError and onSettled functions in the event of a mutation failure and can be useful for rolling back optimistic updates.onSuccess: (data: TData, variables: TVariables, onMutateResult: TOnMutateResult | undefined, context: MutationFunctionContext) => Promise<unknown> | unknown
onError: (err: TError, variables: TVariables, onMutateResult: TOnMutateResult | undefined, context: MutationFunctionContext) => Promise<unknown> | unknown
onSettled: (data: TData, error: TError, variables: TVariables, onMutateResult: TOnMutateResult | undefined, context: MutationFunctionContext) => Promise<unknown> | unknown
retry: boolean | number | (failureCount: number, error: TError) => boolean
0.false, failed mutations will not retry.true, failed mutations will retry infinitely.number, e.g. 3, failed mutations will retry until the failed mutations count meets that number.retryDelay: number | (retryAttempt: number, error: TError) => number
retryAttempt integer and the actual Error and returns the delay to apply before the next attempt in milliseconds.attempt => Math.min(attempt > 1 ? 2 ** attempt * 1000 : 1000, 30 * 1000) applies exponential backoff.attempt => attempt * 1000 applies linear backoff.scope: { id: string }
throwOnError: undefined | boolean | (error: TError) => boolean
true if you want mutation errors to be thrown in the render phase and propagate to the nearest error boundaryfalse to disable the behavior of throwing errors to the error boundary.true) or return the error as state (false)meta: Record<string, unknown>
mutation is available (eg. onError, onSuccess functions of the MutationCache).Parameter2 (QueryClient)
queryClient?: QueryClient
Returns
mutate: (variables: TVariables, { onSuccess, onSettled, onError }) => void
variables: TVariables
mutationFn.onSuccess: (data: TData, variables: TVariables, onMutateResult: TOnMutateResult | undefined, context: MutationFunctionContext) => void
onError: (err: TError, variables: TVariables, onMutateResult: TOnMutateResult | undefined, context: MutationFunctionContext) => void
onSettled: (data: TData | undefined, error: TError | null, variables: TVariables, onMutateResult: TOnMutateResult | undefined, context: MutationFunctionContext) => void
onSuccess will fire only after the latest call you've made.mutateAsync: (variables: TVariables, { onSuccess, onSettled, onError }) => Promise<TData>
mutate but returns a promise which can be awaited.status: MutationStatus
idle initial status prior to the mutation function executing.pending if the mutation is currently executing.error if the last mutation attempt resulted in an error.success if the last mutation attempt was successful.isIdle, isPending, isSuccess, isError: boolean variables derived from statusisPaused: boolean
true if the mutation has been pauseddata: undefined | unknown
undefinederror: null | TError
reset: () => void
failureCount: number
0 when the mutation succeeds.failureReason: null | TError
null when the mutation succeeds.submittedAt: number
0.variables: undefined | TVariables
variables object passed to the mutationFn.undefined.