import {useMutation} from 'blitz'
import updateProject from 'app/projects/mutations/updateProject'
function (props) {
const [updateProjectMutation] = useMutation(updateProject)
return (
<Formik
onSubmit={async values => {
try {
const project = await updateProjectMutation(values)
} catch (error) {
alert('Error saving project')
}
}}>
{/* ... */}
</Formik>
)
}
For more examples and use cases, see the mutation usage documentation.
const [
invoke,
{
data,
error,
isError,
isIdle,
isLoading,
isPaused,
isSuccess,
mutate,
reset,
status,
}
] = useMutation(mutationResolver, {
mutationKey,
onError,
onMutate,
onSettled,
onSuccess,
useErrorBoundary,
})
const promise = invoke(inputArguments, {
onError,
onSettled,
onSuccess,
})
mutationResolver:
A Blitz mutation resolveroptions
mutationKey: string
queryClient.setMutationDefaults
or to identify the mutation in the
devtools.onMutate: (variables: TVariables) => Promise<TContext | void> | TContext | 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, context?: TContext) => Promise<void> | void
onError: (err: TError, variables: TVariables, context?: TContext) => Promise<void> | void
onSettled: (data: TData, error: TError, variables: TVariables, context?: TContext) => Promise<void> | void
retry: boolean | number | (failureCount: number, error: TError) => boolean
false
, failed mutations will not retry by default.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.useErrorBoundary
useErrorBoundary
value, which
is false
[mutate, mutationExtras]
mutate: (variables: TVariables, { onSuccess, onSettled, onError }) => Promise<TData>
useMutation
.variables: TVariables
mutationFn
.useMutation
hook.onSuccess
will fire only after the
latest call you've made.mutationExtras: Object
status: string
idle
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 status
data: undefined | unknown
undefined
error: null | TError
reset: () => void