Hooks API
next-safe-action provides React hooks for executing server actions from client components. All hooks are imported from next-safe-action/hooks (or next-safe-action/stateful-hooks for useStateAction).
useAction
The primary hook for executing server actions from client components.
import { useAction } from "next-safe-action/hooks";
const { execute, result, status, ... } = useAction(safeActionFn, callbacks?);Parameters
Prop
Type
Return object
Prop
Type
Example
"use client";
import { useAction } from "next-safe-action/hooks";
import { createUser } from "./actions";
export function CreateUserForm() {
const { execute, result, isPending, hasSucceeded } = useAction(createUser, {
onSuccess: ({ data }) => {
console.log("Created user:", data.id);
},
onError: ({ error }) => {
console.error("Failed:", error.serverError);
},
});
return (
<form onSubmit={(e) => {
e.preventDefault();
execute({ name: "John", email: "john@example.com" });
}}>
{result.validationErrors?.name && <p>{result.validationErrors.name._errors[0]}</p>}
<button disabled={isPending}>
{isPending ? "Creating..." : "Create User"}
</button>
</form>
);
}useOptimisticAction
Execute actions with optimistic UI updates. The state updates immediately on execute and reverts if the action fails.
import { useOptimisticAction } from "next-safe-action/hooks";
const { execute, optimisticState, ... } = useOptimisticAction(safeActionFn, utils);Parameters
Prop
Type
The utils object also accepts all HookCallbacks (onExecute, onSuccess, onError, onNavigation, onSettled).
Return object
Same as useAction plus:
Prop
Type
Example
"use client";
import { useOptimisticAction } from "next-safe-action/hooks";
import { toggleTodo } from "./actions";
export function TodoItem({ todo }: { todo: Todo }) {
const { execute, optimisticState } = useOptimisticAction(toggleTodo, {
currentState: todo,
updateFn: (state, input) => ({
...state,
completed: !state.completed,
}),
});
return (
<li
style={{ textDecoration: optimisticState.completed ? "line-through" : "none" }}
onClick={() => execute({ id: todo.id })}
>
{optimisticState.title}
</li>
);
}useStateAction (deprecated)
useStateAction is deprecated. Use React's built-in useActionState hook instead.
A stateful hook wrapping React's useActionState. For use with actions defined via .stateAction().
import { useStateAction } from "next-safe-action/stateful-hooks";
const { execute, result, status, ... } = useStateAction(safeStatefulActionFn, utils?);Parameters
Prop
Type
The utils object also accepts HookCallbacks (except onNavigation).
Return object
Same as useAction but without executeAsync and reset.
HookCallbacks
Lifecycle callbacks shared by all hooks:
Prop
Type
onNavigation is not available in useStateAction. Framework navigation functions are handled differently in stateful actions.
HookActionStatus
Union type representing all possible hook states:
type HookActionStatus =
| "idle" // No action executed yet (or after reset)
| "executing" // Action is running on the server
| "transitioning" // React transition after action completes
| "hasSucceeded" // Action completed with data, no errors
| "hasErrored" // Action completed with errors
| "hasNavigated"; // Navigation function was calledHookShorthandStatus
Object of boolean status flags returned alongside the status string:
type HookShorthandStatus = {
isIdle: boolean;
isExecuting: boolean;
isTransitioning: boolean;
isPending: boolean; // isExecuting || isTransitioning
hasSucceeded: boolean;
hasErrored: boolean;
hasNavigated: boolean;
};See also
- Hooks guide — practical guide with patterns and examples
- Optimistic Updates —
useOptimisticActionin depth - Framework Errors —
onNavigationandhasNavigatedbehavior - Type Utilities —
InferUseActionHookReturnand related types