React / 81
FormData and form actions
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:
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 field | What FormData gets |
|---|---|
Has no name | Nothing. It's skipped |
Is disabled | Nothing. Use readOnly if it must be sent |
| Unticked checkbox | Nothing, so check with has() |
Ticked checkbox with no value | "on" |
| Text, textarea, select, the chosen radio | A 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:
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}>2 · React collects
new FormData(form)
name3 · your function
subscribe(formData)
formData.get("email")4a · success
Uncontrolled fields reset
4b · it throws
Nearest error boundary
What FormData actually sees
Try it[...formData.entries()]
- Send the form to see its entries.
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:
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?
| Approach | Values live in | Best for |
|---|---|---|
Controlled + onSubmit | React state | Live validation, previews, fields that depend on each other |
Uncontrolled + onSubmit + FormData | The DOM | Simple forms, any React version |
<form action={fn}> | The DOM | Simple 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.
