Skip to main content

useMutation

The useMutation hook is used to mutate data. Best Practices & Guide

import { useMutation } from '@archibald/client';

const RETURN_VALUE = useMutation(PARAMETERS);
tip

Revalidate the mutated data after a mutation with client.invalidate(key) — it refetches stale-while-revalidate (without clearing first), so useFetch/useSuspenseFetch consumers keep their current content instead of flashing a Suspense fallback. See the Best Practices & Guide.

Parameters

NameTypeDescription
optionsMutateOptions | undefinedThe options that define the behavior of the useMutation hook.

The hook takes no key parameter — a key for the mutation is generated automatically. To use a specific key, set the mutationKey option.

Options

  • Type: MutateOptions

The MutateOptions object has the following properties:

NameTypeDefaultDescription
afterVoidFunction
  • A function that will be executed after the mutate function.
  • Receives params of type DataFunctionParams.
More Info...
beforeVoidFunction
  • A function that will be executed before the mutate function.
  • Receives params of type DataFunctionParams.
More Info...
mutationKeyFetchKeyIf defined it sets a special key for that mutation and doesn't use a generated key. More Info...
ttlnumber15 minutes
  • Defines how long an entry should be kept in the cache.
  • Use -1 for endless caching.
More Info...
errornumber5 secondsDefines how long an error is kept in the cache. More Info...
suspensebooleanfalseDeprecated. Throws the mutation promise during render, which unmounts the form and loses its local state. Drive writes with a transition or <form action> instead. More Info...
errorBoundarybooleanfalseDefines if the error should be thrown. Relies on the deprecated suspense option; prefer { throwOnError: true } on the mutate call inside a transition/error boundary. More Info...
clearKeyAfterMutatebooleanfalseA config to define if the mutation is being cleared directly afterwards. More Info...
enabledbooleantrue
  • Defines if the useMutation hook is active or not.
  • If this is set to false, the action function will not be executed.
More Info...
keepErrorbooleanfalseKeep error in cache when transitioning between pages. More Info...

Return value

The return object has the following properties:

PropertyTypeDescription
dataDATA_TYPE | nullThe data that was retrieved through the hook. This value can be typed by passing a type to the hook.
errorDefaultResponseError | nullThe error that was returned in the action.
isLoadingbooleanReturns true, when the mutation is being executed.
isPendingbooleanAlias of isLoading (React 19 / react-query v5 naming), suitable for transitions and <form action>.
isDonebooleanReturns true when the mutation is finished.
isSuccessbooleanReturns true when the mutation finished successfully.
isErrorbooleanReturns true when the mutation got an error.
mutateFunctionExecutes the provided action and caches the result. Has a stable identity, so it can be passed to <form action={mutate}> or memoized children. Pass { throwOnError: true } per call for a rejecting promise a transition or error boundary can catch.
resetErrorFunctionManually clear the error state from the cache and local state. More Info...
mutationDataMutationThe DataMutation instance.