Skip to content
ClapelloNotes by Barath
ReactCh 14 · 6/6
Core4 min readUpdated

React / 81

FormData and form actions

TL;DR

Skip state and read every named field at once with new FormData(form). React 19 goes further, since form action={fn} handles the submit plumbing for you.

FormData in 30 seconds

FormData is a browser API, not a React one. Hand it a <form> element and it collects every field that has a name:

FeedbackForm.jsx
export default function FeedbackForm() {
  function handleSubmit(e) {
    e.preventDefault();
    const formData = new FormData(e.currentTarget);
    const name = formData.get("name");           // "Asha"
    const toppings = formData.getAll("topping"); // ["cheese", "olives"]
    const offers = formData.has("offers");       // true if ticked
    console.log({ name, toppings, offers });
  }
 
  return (
    <form onSubmit={handleSubmit}>
      <input name="name" aria-label="Name" />
      <label><input type="checkbox" name="topping" value="cheese" /> Cheese</label>
      <label><input type="checkbox" name="topping" value="olives" /> Olives</label>
      <label><input type="checkbox" name="offers" /> Email me offers</label>
      <button type="submit">Send</button>
    </form>
  );
}

The inputs are uncontrolled: no value, no onChange. Give one a starting value with defaultValue (or defaultChecked). For the full idea, see controlled vs uncontrolled.

The fieldWhat FormData gets
Has no nameNothing. It's skipped
Is disabledNothing. Use readOnly if it must be sent
Unticked checkboxNothing, so check with has()
Ticked checkbox with no value"on"
Text, textarea, select, the chosen radioA string
<input type="file">A File object

Need a plain object? Object.fromEntries(formData). Just remember that repeated names (like topping) keep only the last value, so read those with getAll.

React 19: pass a function to action

In plain HTML, action is a URL. In React 19 it can also be a function. React then does the plumbing for you:

NewsletterForm.jsx
export default function NewsletterForm() {
  async function subscribe(formData) {
    const email = formData.get("email");
    await fetch("https://jsonplaceholder.typicode.com/posts", {
      method: "POST",
      body: formData,
    });
    console.log("Subscribed", email);
  }
 
  return (
    <form action={subscribe}>
      <input name="email" type="email" aria-label="Email" required />
      <button type="submit">Subscribe</button>
    </form>
  );
}

No onSubmit, no e.preventDefault(), no new FormData(...). Here's what React does when the user submits:

1 · user submits

Click the submit button or press Enter

<form action={subscribe}>
React stops the page navigation for you

2 · React collects

new FormData(form)

every field that has a name
inside a Transition, can be async

3 · your function

subscribe(formData)

formData.get("email")
when it finishes

4a · success

Uncontrolled fields reset

the form empties, ready for the next entry

4b · it throws

Nearest error boundary

shows its fallback UI, like any render error
With a function in action, React handles the submit plumbing. You write only the function that receives the FormData.

What FormData actually sees

Try it
Toppings name="topping"

[...formData.entries()]

  1. Send the form to see its entries.
Fill everything in and send. The nickname has no name attribute, so it's missing. An unticked box is missing too. A ticked box with no value sends "on". After success, React clears the fields.

Showing "Sending…"

While an async action runs, a button can read the form's status with useFormStatus from react-dom. It must be called in a component rendered inside the form:

SubmitButton.jsx
import { useFormStatus } from "react-dom";
 
export default function SubmitButton() {
  const { pending } = useFormStatus();
  return (
    <button type="submit" disabled={pending}>
      {pending ? "Sending…" : "Send"}
    </button>
  );
}

Which approach when?

ApproachValues live inBest for
Controlled + onSubmitReact stateLive validation, previews, fields that depend on each other
Uncontrolled + onSubmit + FormDataThe DOMSimple forms, any React version
<form action={fn}>The DOMSimple forms in React 19, pending states, server actions

You can mix them: a controlled field still has a name, so it still shows up in FormData.

Gotchas

Recall

Can you answer these?

0/5

Answer out loud (or in your head) before you tap. That's what makes it stick.

Saved on this device only. No login.