本页还没有译文
本页 5 段还没译成한국어,下面显示的是英文原文。useMutation
还没翻译译自英文(tanstack.com)本页 5 段都还没译
const {
data,
error,
isError,
isIdle,
isLoading,
isPaused,
isSuccess,
mutate,
mutateAsync,
reset,
status,
} = useMutation(mutationFn, {
mutationKey,
onError,
onMutate,
onSettled,
onSuccess,
retry,
retryDelay,
useErrorBoundary,
meta,
})
mutate(variables, {
onError,
onSettled,
onSuccess,
})Options
mutationFn: (variables: TVariables) => Promise<TData>- Required
- A function that performs an asynchronous task and returns a promise.
variablesis an object thatmutatewill pass to yourmutationFn
mutationKey: string- Optional
- A mutation key can be set to inherit defaults set with
queryClient.setMutationDefaultsor to identify the mutation in the devtools.
onMutate: (variables: TVariables) => Promise<TContext | void> | TContext | void- Optional
- This function will fire before the mutation function is fired and is passed the same variables the mutation function would receive
- Useful to perform optimistic updates to a resource in hopes that the mutation succeeds
- The value returned from this function will be passed to both the
onErrorandonSettledfunctions 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> | void- Optional
- This function will fire when the mutation is successful and will be passed the mutation's result.
- If a promise is returned, it will be awaited and resolved before proceeding
onError: (err: TError, variables: TVariables, context?: TContext) => Promise<unknown> | void- Optional
- This function will fire if the mutation encounters an error and will be passed the error.
- If a promise is returned, it will be awaited and resolved before proceeding
onSettled: (data: TData, error: TError, variables: TVariables, context?: TContext) => Promise<unknown> | void- Optional
- This function will fire when the mutation is either successfully fetched or encounters an error and be passed either the data or error
- If a promise is returned, it will be awaited and resolved before proceeding
retry: boolean | number | (failureCount: number, error: TError) => boolean- Defaults to
0. - If
false, failed mutations will not retry. - If
true, failed mutations will retry infinitely. - If set to an
number, e.g.3, failed mutations will retry until the failed mutations count meets that number.
- Defaults to
retryDelay: number | (retryAttempt: number, error: TError) => number- This function receives a
retryAttemptinteger and the actual Error and returns the delay to apply before the next attempt in milliseconds. - A function like
attempt => Math.min(attempt > 1 ? 2 ** attempt * 1000 : 1000, 30 * 1000)applies exponential backoff. - A function like
attempt => attempt * 1000applies linear backoff.
- This function receives a
useErrorBoundary: undefined | boolean | (error: TError) => boolean- Defaults to the global query config's
useErrorBoundaryvalue, which isundefined - Set this to
trueif you want mutation errors to be thrown in the render phase and propagate to the nearest error boundary - Set this to
falseto disable the behavior of throwing errors to the error boundary. - If set to a function, it will be passed the error and should return a boolean indicating whether to show the error in an error boundary (
true) or return the error as state (false)
- Defaults to the global query config's
meta: Record<string, unknown>- Optional
- If set, stores additional information on the mutation cache entry that can be used as needed. It will be accessible wherever the
mutationis available (eg.onError,onSuccessfunctions of theMutationCache).
Returns
mutate: (variables: TVariables, { onSuccess, onSettled, onError }) => void- The mutation function you can call with variables to trigger the mutation and optionally override options passed to
useMutation. variables: TVariables- Optional
- The variables object to pass to the
mutationFn.
- Remaining options extend the same options described above in the
useMutationhook. - If you make multiple requests,
onSuccesswill fire only after the latest call you've made.
- The mutation function you can call with variables to trigger the mutation and optionally override options passed to
mutateAsync: (variables: TVariables, { onSuccess, onSettled, onError }) => Promise<TData>- Similar to
mutatebut returns a promise which can be awaited.
- Similar to
status: string- Will be:
idleinitial status prior to the mutation function executing.loadingif the mutation is currently executing.errorif the last mutation attempt resulted in an error.successif the last mutation attempt was successful.
- Will be:
isIdle,isLoading,isSuccess,isError: boolean variables derived fromstatusdata: undefined | unknown- Defaults to
undefined - The last successfully resolved data for the query.
- Defaults to
error: null | TError- The error object for the query, if an error was encountered.
reset: () => void- A function to clean the mutation internal state (i.e., it resets the mutation to its initial state).
AI 翻译查看英文原文