All posts
reactfrontend

React 19 Actions Replaced My Form Boilerplate

Pending flags, error state, optimistic rows, and a reset after submit. I used to write all of it by hand for every form. React 19 turned most of it into three hooks and a function passed to the form.

Muhamad Choirul Adam 3 min readPhoto: Unsplash

Every form I built before last month had the same skeleton. A useState for the pending flag, another for the error, an onSubmit that called preventDefault, a try/catch/finally, and a manual reset when it worked. Multiply that by every dashboard with ten forms and it's a lot of code that says nothing about the feature.

React 19 shipped stable in December. The headline feature is Actions, and after a few weeks of moving forms over, most of that skeleton is gone.

What an Action is

An Action is just an async function you hand to React, usually through a form's action prop. React runs it in a transition, tracks whether it's pending, and resets uncontrolled form fields when it finishes.

Rendering diagram…

The before and after, for a simple "add note" form:

before.tsx
function AddNote() {
  const [pending, setPending] = useState(false);
  const [error, setError] = useState<string | null>(null);
 
  async function onSubmit(e: React.FormEvent<HTMLFormElement>) {
    e.preventDefault();
    setPending(true);
    setError(null);
    try {
      await saveNote(new FormData(e.currentTarget));
      e.currentTarget.reset();
    } catch (err) {
      setError("Could not save the note.");
    } finally {
      setPending(false);
    }
  }
 
  return <form onSubmit={onSubmit}>{/* fields, button, error */}</form>;
}
after.tsx
function AddNote() {
  const [error, submit, pending] = useActionState(async (_prev: string | null, data: FormData) => {
    const ok = await saveNote(data);
    return ok ? null : "Could not save the note.";
  }, null);
 
  return <form action={submit}>{/* fields, button, error */}</form>;
}

Same behavior. The state machine I kept rewriting is now React's job.

The three hooks I actually use

HookWhat it replaces
useActionStateThe pending flag, the error state, and the try/catch
useFormStatusProp drilling pending into a submit button
useOptimisticHand-written "show it now, roll back if it fails" code

useFormStatus is the small one I like most. A submit button can read the status of the form it sits in, so a shared <SubmitButton> disables itself without anyone passing it a prop:

submit-button.tsx
function SubmitButton({ children }: { children: React.ReactNode }) {
  const { pending } = useFormStatus();
  return (
    <button type="submit" disabled={pending}>
      {pending ? "Saving…" : children}
    </button>
  );
}

useOptimistic is the one that changed how lists feel. Add a row, show it immediately in a faded state, and React swaps it for the real one when the action finishes, or drops it if the action throws.

What didn't change

  • Validation still belongs on the server. Actions make the client code shorter. They don't make client checks trustworthy.
  • Controlled inputs still work. Field reset only applies to uncontrolled inputs, so a form that mirrors every keystroke into state still needs its own reset.
  • Complex forms still want a form library. Multi-step wizards with field-level rules are still easier with one. The win is that simple forms no longer need it.

Why it matters

The forms in an admin panel are rarely interesting. They're the part of the codebase that grows fastest and gets read least. Deleting the boilerplate from them means the code that's left is the part that's actually about the feature: what gets saved, and what the user sees when it doesn't.