Lesson 164 of 296
Forms: controlled vs uncontrolled inputs
What you will learn
- Controlled inputs
- Uncontrolled inputs with refs or FormData
- Which to choose
- Checkboxes, selects and multiple fields
- Form actions in React 19
On this page
Controlled inputs
A controlled input gets its value from React state, and every change goes through a handler. React is the single source of truth.
function NameField() {
const [name, setName] = useState("");
return (
<div>
<label htmlFor="name">Name</label>
<input id="name" value={name} onChange={(e) => setName(e.target.value)} />
<p>Hello, {name || "stranger"}</p>
</div>
);
}valuesets what the input shows;onChangeupdates state on every keystroke.- If you set
valuewithoutonChange, the input is read-only (React warns you).
Good for: instant validation, formatting while typing, enabling a button only when valid, showing the value elsewhere.
Uncontrolled inputs
An uncontrolled input keeps its own value in the DOM, like normal HTML. You read it when you need it: on submit.
function NewsletterForm({ onSubscribe }: { onSubscribe: (email: string) => void }) {
function handleSubmit(e: FormEvent<HTMLFormElement>) {
e.preventDefault();
const data = new FormData(e.currentTarget);
onSubscribe(String(data.get("email") ?? ""));
e.currentTarget.reset();
}
return (
<form onSubmit={handleSubmit}>
<label htmlFor="nl-email">Email</label>
<input id="nl-email" name="email" type="email" defaultValue="" required />
<button type="submit">Subscribe</button>
</form>
);
}- Use
defaultValue(ordefaultChecked) for the starting value, notvalue. - Read values with
FormData(Lesson 89) or a ref (Lesson 167).
Good for: simple forms, file inputs (<input type="file"> is always uncontrolled), and performance with very large forms (no re-render per keystroke).
Which to choose?
| Controlled | Uncontrolled | |
|---|---|---|
| Source of truth | React state | The DOM |
| Re-renders while typing | Yes | No |
| Instant validation and formatting | Easy | Harder |
| Code amount | More | Less |
| File inputs | Not possible | Yes |
Form libraries such as React Hook Form (Lesson 174) use mostly uncontrolled inputs for performance, while giving you a controlled-style API.
One state object for many fields
type SignupForm = { name: string; email: string; plan: "free" | "pro"; terms: boolean };
function Signup({ onSubmit }: { onSubmit: (form: SignupForm) => void }) {
const [form, setForm] = useState<SignupForm>({ name: "", email: "", plan: "free", terms: false });
function update<K extends keyof SignupForm>(key: K, value: SignupForm[K]) {
setForm((f) => ({ ...f, [key]: value }));
}
return (
<form onSubmit={(e) => { e.preventDefault(); onSubmit(form); }}>
<input aria-label="Name" value={form.name} onChange={(e) => update("name", e.target.value)} />
<input aria-label="Email" type="email" value={form.email} onChange={(e) => update("email", e.target.value)} />
<select aria-label="Plan" value={form.plan} onChange={(e) => update("plan", e.target.value as SignupForm["plan"])}>
<option value="free">Free</option>
<option value="pro">Pro</option>
</select>
<label>
<input type="checkbox" checked={form.terms} onChange={(e) => update("terms", e.target.checked)} />
I accept the terms
</label>
<button type="submit" disabled={!form.terms || form.name.trim() === ""}>Sign up</button>
</form>
);
}- Checkboxes use
checkedande.target.checked, notvalue. <select>usesvalueon the select, notselectedon options.- The generic
updatefunction (Lesson 110) keeps each field's type correct.
Form actions in React 19
React 19 lets a form's action prop be a function. React calls it with the FormData on submit, and hooks such as useActionState and useFormStatus track pending state and results. This works especially well with Next.js server actions (Part 13). You will see both styles in codebases; know that both exist.
How to say it in an interview
"A controlled input gets its value from state and updates it in onChange, which makes React the source of truth and suits instant validation and formatting. An uncontrolled input keeps its value in the DOM with defaultValue, and I read it on submit with FormData or a ref, which is simpler and avoids re-rendering on every keystroke; file inputs are always uncontrolled. Checkboxes use checked. React 19 also supports passing a function as a form action, used with useActionState and server actions."
Key words
| Word | Meaning |
|---|---|
| Controlled input | Value comes from React state |
| Uncontrolled input | Value is kept in the DOM |
| defaultValue | The starting value of an uncontrolled input |
| FormData | Reads all named fields of a form |
| Form action | A function React calls with the form data on submit |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.