useActionState

React chapter 21 · React 19 · form actions · source: react.dev/reference/react/useActionState

The problem

A form needs "is it saving?", "did it fail?" and "what was saved?". Written by hand that is several useStates plus an onSubmit, and a button in a child component cannot see "saving" at all.

The pattern that fixes it

useActionState(action, initial) returns [state, formAction, isPending]. Give formAction to <form action>, and a child can read pending with useFormStatus(), no props needed.

In plain words

Submitting a form is a little story: you click, you wait, you get a result or an error. useActionState lets you write that story as one function and React keeps track of the waiting and the result for you.

Ordering at a counter. Writing it by hand, you keep a notepad ("order placed? paid? error?"). With an action, the cashier owns the notepad and just tells you "still working" or "here is your result".

Terms

action
A function you hand to a form. React calls it with the form data when the form is submitted, and it may be async.
pending
True while the action is still running (waiting for the server).
useFormStatus
A hook a component inside a form uses to read that form's pending status without receiving a prop.
reducer-style state
The action gets the previous state and returns the next state, like a useReducer that can wait.

Going deeper

How does it work?

On submit React calls your action as action(previousState, formData). While the returned promise is unsettled, isPending is true. When it settles, whatever it returned becomes the new state. If the user submits again while one is running, React queues the calls and runs them in order, each one receiving the state produced by the one before.

Pitfalls

useFormStatus only sees a form it is inside of, so call it in a child component, never in the component that renders the <form>. It also reports nothing for a form that uses onSubmit instead of action. After a successful action React resets uncontrolled inputs, so return the typed text in your state and use it as defaultValue if you want it kept. A plain submit button does not disable itself: queued double-submits are safe but usually not what you want, so disable on pending.

When not to use it

For one-off clicks that are not forms (a "Like" button), a plain async handler or useTransition is simpler. For live validation on every keystroke keep a controlled input. For server data that many components share, a data library (TanStack Query, framework loaders) gives caching and retries that an action does not. The React Compiler does not replace this hook; it solves a different problem (re-renders).

Keep learning

Overview

snap, async save(note: string): Promise request sent response: saved response: too short Save note Write at least 3 characters. at least 3 characters Requests sent and requests running at the same time requests sent running right now most at once What really happened, in order (a fake server that answers after 900 ms): nothing yet: press Save note Real life: a notes form. The Save button lives in its own little component, like in any real design system. , hot: true, note:

Press Save note three times quickly, then watch the bars.

Several requests run at once because the button has no idea that one is already running. You wrote the pending bookkeeping by hand, and the button would need pending passed to it as a prop. Type hello and press Save note three times fast. Read "most at once" in the bars. Type just a and save. Watch error appear in the state chips after the response arrives.

useFormStatus sees nothing here.

This form uses onSubmit, not action, so React does not know it is submitting. The button never disables, and requests pile up.

Action works, but a blind button still lets you queue submits.

React runs the queued submits one after another (watch "running right now" stay at 1), but you probably do not want three saves. Disable on pending with useFormStatus.

Save is disabled while saving, with no pending prop passed.

isPending comes from the hook, the button reads it with useFormStatus, and the error and saved note are just the action's returned state. Same notes form. Swap the approach, then swap the button, and try to submit twice. How the form is wired useState + onSubmit Which submit button Plain button Reads useFormStatus Reset lab , hot: true, note: React knows nothing <button type="submit">Save</button> never disabled With useActionState + Reads useFormStatus, press Save three times: only the first goes through, then the button re-enables. Switch to Plain button, do the same, and compare "requests sent". Switch to useState + onSubmit with the status button to see why action matters.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.