Action state and pending UI
What you need to know
A mutation changes server data. Action state lets a form show the result of that operation and its pending state without pretending that submission equals success. In Next.js, these React hooks run in a Client Component even when the action itself is a Server Function.
By the end, you can: show pending and expected errors; prevent duplicate submission.
A public title-check form
// app/title-check/page.tsx — client-only teaching form; no persistence'use client';import { useActionState, useState } from 'react';type State = { message: string };async function check(_: State, data: FormData): Promise<State> { const value = data.get('title'); return { message: typeof value === 'string' && value.trim().length >= 3 ? 'Title accepted for review' : 'Enter at least three characters' };}export default function Page() { const [state, action, pending] = useActionState(check, {message: ''}); const [title, setTitle] = useState(''); return <form action={action}> <label htmlFor="title">Title</label><input id="title" name="title" value={title} onChange={e => setTitle(e.target.value)} /> <button disabled={pending}>{pending ? 'Checking…' : 'Check'}</button> <p role="status">{state.message}</p> </form>;}Expected behavior: Entering Oak returns the acceptance message, while A returns the validation message. This action executes on the client and is not server-side enforcement.
How it works
useActionState(action, initialState) returns a state value, a dispatch function and a pending flag. The action receives previous state before the form data. Return expected validation errors as ordinary serializable values; unexpected failures belong in error handling. React can reset uncontrolled fields after a normally resolved action even when its returned value describes invalid input. The controlled title in this demo retains the entered text. Use a discriminated state such as idle, invalid and saved. The server still validates every input and verifies the caller before a protected write. A pending button improves interaction, but two tabs, retries or direct requests can submit duplicates. For operations that must happen once, persist an idempotency key with the result in the same transaction, scoped to the caller and operation. An identical key with different input should be rejected rather than silently reused. The first example validates a public demo title without persistence; the second demonstrates the parent-form relationship of the React DOM hook.
Read pending state from a descendant
// app/check/Submit.tsx — render inside the form from its parent'use client';import { useFormStatus } from 'react-dom';export default function Submit() { const {pending} = useFormStatus(); return <button type="submit" disabled={pending}> {pending ? 'Submitting…' : 'Submit'} </button>;}Expected behavior: When placed inside the relevant form, the control reflects that form’s submission. A very fast action may finish before a pending frame is visible.
Common mistake
Calling useFormStatus in a component that renders the form itself does not observe that new form. Move the hook into a descendant, or use the pending value from useActionState. Do not equate disabled controls with duplicate-write protection.
Practice and verification
Write down the transitions idle → pending → accepted or invalid. Add a slow synthetic action only in an isolated browser lab and check both paths. Design a stored idempotency key contract for an inventory reservation, without creating any real reservation.
Scope: App Router, proposed Next.js 16.x, React 19 family, Cache Components disabled. Framework snippets are static or explicitly partial; actual browser, auth and generated-route checks are pending. SQLite exercises run separately in the authoring environment, not the ByteMind evaluator.
Further reading: Forms · Action State · Form Status · Mutating