Skip to content

API reference > @kontsedal/olas-react > useMutation

useMutation() function ​

Subscribe to all signals on a Mutation<V, R> with a single useSyncExternalStore call. Returns the observable values plus two triggers:

  • mutate(vars) for an event handler. It returns nothing, and a failure surfaces on error / status and through onError. - run(vars) when the caller needs the result. It returns the run's promise, which rejects on failure.

The hook is a subscription layer: concurrency (latest-wins, serial, …) is configured on the mutation in the controller.

Signature:

typescript
export declare function useMutation<V, R>(mutation: Mutation<V, R>, callbacks?: UseMutationCallbacks<V, R>): UseMutationResult<V, R>;

Parameters ​

Parameter

Type

Description

mutation

Mutation<V, R>

callbacks

UseMutationCallbacks<V, R>

(Optional)

Returns:

UseMutationResult<V, R>

Example ​

tsx
function SaveButton({ save, draft }: { save: Mutation<string, void>; draft: string }) {
  const { mutate, isPending, isError } = useMutation(save)
  return (
    <button type="button" disabled={isPending} onClick={() => mutate(draft)}>
      {isError ? 'Retry' : 'Save'}
    </button>
  )
}

Released under the MIT License.