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,
{
status,
isIdle,
isLoading,
isSuccess,
isError,
data,
error,
reset
}
] = useMutation(mutationResolver, {
onMutate,
onSuccess,
onError,
onSettled,
throwOnError,
useErrorBoundary,
})
const promise = invoke(inputArguments, {
onSuccess,
onSettled,
onError,
throwOnError,
})
mutationResolver:
A Blitz mutation resolveroptions
onMutate: Function(inputArguments) => Promise | snapshotValue
onError
and onSettled
functions in the event of a mutation failure
and can be useful for rolling back optimistic updates.onSuccess: Function(data, inputArguments) => Promise | undefined
mutate
-level onSuccess
handler (if it is defined)onError: Function(err, inputArguments, onMutateValue) => Promise | undefined
mutate
-level onError
handler (if it is defined)onSettled: Function(data, error, inputArguments, onMutateValue) => Promise | undefined
mutate
-level onSettled
handler (if it is defined)throwOnError
true
false
if failed mutations should not re-throw errors
from the mutation function to the mutate
function.useErrorBoundary
[invoke, mutationExtras]
invoke: Function(inputArguments, { onSuccess, onSettled, onError, throwOnError }) => Promise
inputArguments: any
mutationResolver
.useMutation
hook.useMutation
-level options.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 | Any
undefined
error: null | Error
reset: Function() => void