function useMutation<TData, TError, TVariables, TOnMutateResult>(options, queryClient?): UseMutationResult<TData, TError, TVariables, TOnMutateResult>;Defined in: preact-query/src/useMutation.ts:87
Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects. useMutation is the hook for that.
TData = unknown
TError = Error
TVariables = void
TOnMutateResult = unknown
UseMutationOptions<TData, TError, TVariables, TOnMutateResult>
The UseMutationOptions to use — everything you can pass to useMutation.
QueryClient
Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
UseMutationResult<TData, TError, TVariables, TOnMutateResult>
mutate/mutateAsync also accept per-call onSuccess/onError/onSettled callbacks as a second argument, useful for triggering call-site side effects (e.g. navigation) without coupling them to the shared mutation definition. If you make multiple requests, onSuccess will fire only after the latest call you've made.
mutationOptions to share these options across multiple useMutation call sites.
import { useMutation, useQueryClient } from '@tanstack/preact-query'
function AddTodo() {
const queryClient = useQueryClient()
const addMutation = useMutation({
mutationFn: addTodo,
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
})
return (
<button onClick={() => addMutation.mutate('Item')}>Add</button>
)
}Optimistic update via onMutate, rolling back on onError:
import { useMutation, useQueryClient } from '@tanstack/preact-query'
function AddTodo() {
const queryClient = useQueryClient()
const addMutation = useMutation({
mutationFn: addTodo,
onMutate: async (newTodo) => {
await queryClient.cancelQueries({ queryKey: ['todos'] })
const previousTodos = queryClient.getQueryData<Array<string>>(['todos'])
queryClient.setQueryData<Array<string>>(['todos'], (old) => [
...(old ?? []),
newTodo,
])
// Passed to `onError` as `context` if the mutation fails.
return { previousTodos }
},
onError: (_err, _newTodo, context) => {
queryClient.setQueryData(['todos'], context?.previousTodos)
},
onSettled: () => {
queryClient.invalidateQueries({ queryKey: ['todos'] })
},
})
return (
<button onClick={() => addMutation.mutate('Item')}>Add</button>
)
}