safe-action-hooks

Implements next-safe-action React hooks for executing server actions with status tracking and optimistic updates.

Updated Jul 8, 2026
One-click install
npx skills add https://github.com/NarixHine/uni-chem --skill safe-action-hooks-narixhine
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: safe-action-hooks
Source: https://github.com/NarixHine/uni-chem/tree/main/.agents/skills/safe-action-hooks
Command: npx skills add https://github.com/NarixHine/uni-chem --skill safe-action-hooks-narixhine

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires next-safe-action, zod.

What problem does it solve? Calling next-safe-action server actions from React client components requires managing execution state, results, errors, and navigation behavior manually. This Skill provides the correct patterns for useAction, useOptimisticAction, and useStateAction so you avoid type errors and broken UI states. ## Core Features & Use Cases - useAction execution patterns: Covers execute vs executeAsync, the discriminated-union result object, status shorthands (isPending, hasSucceeded, hasErrored), and lifecycle callbacks (onExecute, onSuccess, onError, onNavigation, onSettled). - Optimistic UI updates: Shows how useOptimisticAction wraps React.useOptimistic with currentState and updateFn for instant like buttons, todo toggles, and list add/delete operations. - Stateful form actions: Explains useStateAction with the formAction dispatcher, initResult seeding, and the required .stateAction() server-side definition with prevResult access. - Use Case: You are building a feedback form in a Next.js 15 app and need validation errors displayed per field, a pending submit button, and a success message. Use this Skill to wire useStateAction with a .stateAction() server action correctly. ## Quick Start Ask the AI to show how to call a next-safe-action server action from a client component using useAction with success and error callbacks.

Frequently Asked Questions about safe-action-hooks

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I use useAction from next-safe-action in a client component?▼

Import useAction from next-safe-action/hooks, pass your server action and optional callbacks like onSuccess and onError, then call execute(input) from an event handler. The hook returns result, status, and isPending for rendering UI states.

What is the difference between execute and executeAsync in next-safe-action?▼

execute is fire-and-forget and reports outcomes through callbacks and the result state. executeAsync returns a Promise of the SafeActionResult for inline awaiting, but it throws on navigation errors, so wrap it in try/catch and rethrow navigation errors.

useAction vs useStateAction: which hook should I use for forms?▼

Default to useAction unless you need the <form action={formAction}> pattern or prevResult access on the server. useStateAction requires the action to be defined with .stateAction() and React 19, while useAction works with .action().

Why does useStateAction give a type error with my server action?▼

The server action was defined with .action() instead of .stateAction(). useStateAction only accepts stateful actions that receive prevResult as a second argument, so redefine the action using .stateAction().

How do optimistic updates work with useOptimisticAction?▼

Pass currentState from the server and a pure updateFn that computes the optimistic state from the input. The UI updates synchronously on execute, then reverts to the real server state after revalidation or on error.

Why are onNavigation and onSettled unavailable with throwOnNavigation?▼

When throwOnNavigation is true, navigation errors are thrown during React's render phase, so effects and callbacks cannot run. TypeScript enforces this via a discriminated union; use server-side action callbacks for side effects instead.