Handling forms in React 19

React 19 came out on December 5, and the headline feature is Actions.

React 19 came out on December 5, and the headline feature is Actions. For forms, that means much of the usual boilerplate goes away: the isSubmitting state, the error state, preventDefault(), and controlled inputs for fields you only read on submit.

A function as the form action

You can now pass a function to a form’s action:

function Newsletter() {
  async function subscribe(formData) {
    await fetch('/api/subscribe', { method: 'POST', body: formData });
  }

  return (
    <form action={subscribe}>
      <input name="email" type="email" required />
      <button>Subscribe</button>
    </form>
  );
}

React calls subscribe with the form’s FormData when it’s submitted. No onSubmit, no preventDefault(), no useState per field. After the action finishes successfully, React also resets the form’s uncontrolled fields for you.

The browser’s built-in validation, like required and type=“email”, still runs first.

useActionState: results and errors

Most forms need to show something after submitting: an error, a success message. useActionState wraps an action and keeps its last result as state:

import { useActionState } from 'react';

async function updateProfile(prevState, formData) {
  const name = String(formData.get('name') ?? '').trim();
  const email = String(formData.get('email') ?? '').trim();

  if (!name) return { error: 'Name is required', savedAt: null };

  try {
    await saveProfile({ name, email });
    return { error: null, savedAt: Date.now() };
  } catch {
    return { error: 'Could not save. Try again.', savedAt: prevState.savedAt };
  }
}

function ProfileForm() {
  const [state, formAction, isPending] = useActionState(updateProfile, {
    error: null,
    savedAt: null,
  });

  return (
    <form action={formAction}>
      <input name="name" placeholder="Name" />
      <input name="email" type="email" placeholder="Email" />
      <SubmitButton />
      {isPending && <p>Saving...</p>}
      {state.error && <p role="alert">{state.error}</p>}
      {state.savedAt && <p>Saved.</p>}
    </form>
  );
}

A few things are different from a normal event handler:

  • The action gets the previous state as its first argument, and FormData as the second.
  • Whatever it returns becomes the new state.
  • isPending is true while it runs. You don’t manage that yourself.

Notice that errors are returned, not thrown. A thrown error goes to the nearest error boundary, which is right for a bug, but not for “the name is empty”. Return expected errors as state.

If you saw this hook in the release candidate as useFormState from react-dom, it has been renamed and moved to react.

useFormStatus for the submit button

A submit button usually needs to know if its form is pending. Passing isPending down works, but React 19 has a hook for it:

import { useFormStatus } from 'react-dom';

function SubmitButton() {
  const { pending } = useFormStatus();
  return (
    <button type="submit" disabled={pending}>
      {pending ? 'Saving...' : 'Save'}
    </button>
  );
}

The catch: useFormStatus reads the status of the form it’s rendered inside. Calling it in the same component that renders the

doesn’t work. It always reports pending: false. It has to be in a child component, like SubmitButton here. That makes it perfect for a shared button in a design system.

useOptimistic: show it before the server answers

For things like comments or likes, you want the UI to update instantly, and to undo the change if the request fails. useOptimistic gives you a temporary version of some state that only lives while an action runs:

import { useOptimistic, useState } from 'react';

function Comments({ initial }) {
  const [comments, setComments] = useState(initial);
  const [optimisticComments, addOptimistic] = useOptimistic(
    comments,
    (current, text) => [
      ...current,
      { id: `temp-${current.length}`, text, sending: true },
    ],
  );

  async function send(formData) {
    const text = String(formData.get('text'));
    addOptimistic(text);
    const saved = await postComment(text);
    setComments((list) => [...list, saved]);
  }

  return (
    <>
      <ul>
        {optimisticComments.map((c) => (
          <li key={c.id} style={{ opacity: c.sending ? 0.5 : 1 }}>
            {c.text}
          </li>
        ))}
      </ul>
      <form action={send}>
        <input name="text" />
        <button>Post</button>
      </form>
    </>
  );
}

The new comment shows up greyed out straight away. When the action finishes, the optimistic version is dropped and React shows the real comments state, which now includes the saved one. If postComment throws, the optimistic comment simply disappears. You’d still want to show an error, but there’s nothing to roll back by hand.

Do you still need a form library?

For simple and medium forms like login, settings or contact pages, these hooks cover most of what I used a library for. Libraries still help with complex client-side validation, field arrays, and showing errors as the user types, since Actions only run on submit.

And if you use a framework with Server Functions, the same action prop accepts a function that runs on the server, so the form works even before the JavaScript has loaded. In a plain client-side React app, everything above works the same way.