
const {data,error,isError,isIdle,isLoading,isPaused,isSuccess,mutate,mutateAsync,reset,status,} = useMutation(mutationFn, {cacheTime,mutationKey,networkMode,onError,onMutate,onSettled,onSuccess,retry,retryDelay,useErrorBoundary,meta})mutate(variables, {onError,onSettled,onSuccess,})
Options
mutationFn: (variables: TVariables) => Promise<TData>variables is an object that mutate will pass to your mutationFncacheTime: number | InfinityInfinity, will disable garbage collectionmutationKey: stringqueryClient.setMutationDefaults or to identify the mutation in the devtools.networkMode: 'online' | 'always' | 'offlineFirst'online'onMutate: (variables: TVariables) => Promise<TContext | void> | TContext | voidonError and onSettled functions in the event of a mutation failure and can be useful for rolling back optimistic updates.onSuccess: (data: TData, variables: TVariables, context?: TContext) => Promise<unknown> | voidonError: (err: TError, variables: TVariables, context?: TContext) => Promise<unknown> | voidonSettled: (data: TData, error: TError, variables: TVariables, context?: TContext) => Promise<unknown> | voidretry: boolean | number | (failureCount: number, error: TError) => boolean0.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) => numberretryAttempt 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.useErrorBoundary: undefined | boolean | (error: TError) => booleanuseErrorBoundary value, which is undefinedtrue 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).context?: React.Context<QueryClient | undefined>defaultContext will be used.Returns
mutate: (variables: TVariables, { onSuccess, onSettled, onError }) => voiduseMutation.variables: TVariablesmutationFn.useMutation hook.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: stringidle initial status prior to the mutation function executing.loading 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, isLoading, isSuccess, isError: boolean variables derived from statusisPaused: booleantrue if the mutation has been pauseddata: undefined | unknownundefinederror: null | TErrorreset: () => voidThe best JavaScript newsletter! Delivered every Monday to over 76,000 devs.