الانتقال إلى المحتوى
FrontendReact· 7 دقائق للقراءة

React 19 Form Actions: Drop the isLoading Boilerplate

Pass a function to the form's action and let useActionState and useFormStatus track the result and the pending state for you.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • React 19
محتويات المقال

A team's invite form has the usual three pieces of state: the email, isLoading and error. One afternoon the invite API times out and throws. The setIsLoading(false) after the await never runs, and the button says "Sending…" until someone reloads the page. Another form in the same app copied the pattern but forgot setError(null), so an old error stays on screen after a retry succeeds. Neither bug is about invites. Both live in plumbing that every form rewrites.

The common misconception

The belief: every React form needs an onSubmit handler that calls e.preventDefault(), a state variable per field, and isLoading and error states that you set by hand before and after the await.

For years that was simply how React forms were written, and it still works. React's own announcement of version 19 opens its Actions section with hand-written pending and error state as the "before" example. It's explicit, every tutorial teaches it, and many developers assume the action prop is only for Server Functions in a framework. The half that's true: the UI does need a pending state and somewhere to put errors. What changed is who keeps them in sync.

What actually happens

A function in action becomes an Action

React 19 lets you pass a function to a form's action prop. On submit, React calls it with the form's FormData, and the function may be async. It runs in a , so unlike onSubmit there's no e.preventDefault() to remember, and once it succeeds, React resets the form's uncontrolled fields. By React's convention, functions that use async transitions are called . The article on controlled and uncontrolled inputs showed how to read the fields from FormData; this one is about the state around the submission.

useActionState keeps the result and the pending flag

useActionState(action, initialState) returns three things: the current state, a function to give to the form's action, and isPending, which is true while its actions run. Your action receives the previous state first and the FormData second, and whatever it returns becomes the new state. That's where expected errors go: the docs suggest returning validation messages as part of the state. Unexpected errors can simply be thrown: React cancels any queued actions and shows the nearest Error Boundary.

TSX InviteForm.tsx
import { useState, type FormEvent } from "react";export function InviteForm() {  const [email, setEmail] = useState("");  const [isLoading, setIsLoading] = useState(false);  const [error, setError] = useState<string | null>(null);  async function handleSubmit(e: FormEvent<HTMLFormElement>) {    e.preventDefault();    setIsLoading(true);    setError(null);    const result = await sendInvite(email); // a throw skips the next lines    setError(result.error ?? null);    setIsLoading(false);  }  return (    <form onSubmit={handleSubmit}>      <input value={email} onChange={(e) => setEmail(e.target.value)} />      <button disabled={isLoading}>{isLoading ? "Sending…" : "Invite"}</button>      {error && <p role="alert">{error}</p>}    </form>  );}

Both bugs from the opening are gone. There's no setIsLoading pair to break: isPending ends when the Transition does, so a timeout goes to the nearest Error Boundary instead of freezing the button. And the error message is just the latest returned state, so a successful retry replaces it. React also queues submissions and runs them one after another, each receiving the previous result, so a double click sends two invites in turn instead of racing them.

useFormStatus for buttons deep inside the form

A shared submit button shouldn't need an isPending prop threaded into it. useFormStatus from react-dom returns the status of the form the component is rendered in:

TSX SubmitButton.tsx
import { useFormStatus } from "react-dom";// Disables itself inside any <form action>, no props neededexport function SubmitButton({ label }: { label: string }) {  const { pending } = useFormStatus();  return <button disabled={pending}>{pending ? "Saving…" : label}</button>;}

It only sees a parent <form>. Called in the same component that renders the form, pending is never true, so it has to live in a child component like this one.

useOptimistic for instant feedback

Some actions should look done before the server answers, like a like button or a renamed item. useOptimistic(value) returns a value you can override inside an Action. The override shows while the Action is pending; when it ends, React renders the real value in the same render, and if the Action throws, the screen falls back to whatever the real value is.

ConcernonSubmit and useStateReact 19 Actions
Stopping the page reloade.preventDefault() every timeNot needed with a function action
Pending stateisLoading set around the awaitisPending, or pending from useFormStatus
Expected errorsA separate state you clear yourselfPart of the state the action returns
Unexpected errorstry/finally, or a stuck spinnerThrown to the nearest Error Boundary
Clearing fields after successReset each state by handUncontrolled fields reset automatically

How to apply it in your project

  1. Find the plumbingSearch for preventDefault() in submit handlers and for setIsLoading(true) paired with setIsLoading(false). Each pair is a candidate.
  2. Turn the handler into an actionWrite a function that takes the previous state and the FormData, calls your API and returns the next state, including any expected error. Pass it through useActionState.
  3. Move pending UI into the form's childrenReplace isLoading with isPending, and give shared submit buttons useFormStatus instead of a prop.
  4. Decide what survives an errorReturn the submitted values with the error, or keep those fields controlled, and wrap the form in an Error Boundary for failures you didn't expect.

Key takeaways

  • A function passed to <form action> runs as an Action: it gets FormData, needs no preventDefault and runs in a Transition.
  • useActionState turns what the action returns into state and gives you isPending; no flags to set or forget.
  • Return expected errors as state, throw unexpected ones to an Error Boundary, and give fields their values back on error.
  • useFormStatus gives nested buttons the pending state; useOptimistic shows the result before the server confirms.

📚 المصادر

  1. <form> — React DOM Components
  2. useActionState — React API Reference
  3. useFormStatus — React DOM Hooks
  4. useOptimistic — React API Reference
  5. React v19 — React Blog

التعليقات (0)