useMutation

React hook to execute asynchronous mutations with loading, error, and data states alongside success and error callbacks.

pnpm add react-callback-hooks

Demo

Usage

Shorthand — mutationFn + optional callbacks:

import { useMutation } from 'react-callback-hooks'

type NewPost = { title: string; body: string }
type Post = NewPost & { id: number }

const [createPost, { data, loading, error, reset }] = useMutation<
  Post,
  NewPost
>(
  (variables) =>
    fetch('/api/posts', {
      method: 'POST',
      body: JSON.stringify(variables)
    }).then((r) => r.json()),
  {
    onSuccess: (data, variables) => console.log('created', data.id),
    onError: (err, variables) => console.error(err)
  }
)

createPost({ title: 'Hello', body: 'World' })

Object form:

const [deleteUser, state] = useMutation<void, number>({
  mutationFn: (id) =>
    fetch(`/api/users/${id}`, { method: 'DELETE' }).then((r) => r.json()),
  onSuccess: (_, id) => console.log('deleted', id),
  onError: (err) => console.error(err)
})

deleteUser(42)

Parameters

Shorthand form

ParameterTypeDescription
mutationFn(variables: V) => Promise<T>Async function that performs the mutation.
callbacks.onSuccess(data: T, variables: V) => voidCalled when the mutation resolves.
callbacks.onError(error: Error, variables: V) => voidCalled when the mutation rejects.

Object form

PropertyTypeDescription
mutationFn(variables: V) => Promise<T>Async function that performs the mutation.
onSuccess(data: T, variables: V) => voidCalled when the mutation resolves.
onError(error: Error, variables: V) => voidCalled when the mutation rejects.

Return Values

Returns a tuple [mutate, state]:

TypeDescription
mutate(variables: V) => Promise<void>Triggers the mutation with the given variables.
state.dataT | nullLast resolved value, or null before first success.
state.loadingbooleantrue while the mutation is in flight.
state.errorError | nullLast error, or null if the last call succeeded.
state.reset() => voidResets data, loading and error to their initial values.