Skip to main content

Documentation Index

Fetch the complete documentation index at: https://kubo-47e69177.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

useAction

Signature

packages/react/src/useAction.ts
export function useAction<TInput, TOutput>(
  action: DefinedAction<TInput, TOutput>
): UseActionResult<TInput, TOutput>

Returned state

FieldTypeDescription
execute(input: TInput) => Promise<TOutput>Executes the action
dataTOutput | undefinedLast successful value
errorunknownLast thrown error
statusidle | pending | success | errorCurrent state
reset() => voidResets state to idle

Example

components/create-user-form.tsx
"use client";

import { useAction } from "@zapaction/react";

import { createUser } from "../app/actions";

export function CreateUserForm() {
  const action = useAction(createUser);

  return (
    <form
      onSubmit={async (event) => {
        event.preventDefault();
        await action.execute({ email: "hello@example.com" });
      }}
    >
      <button disabled={action.isPending}>Create</button>
      {action.isError ? <p>Request failed</p> : null}
    </form>
  );
}