Skip to the content

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.

TSX
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>
  );
}
  • value sets what the input shows; onChange updates state on every keystroke.
  • If you set value without onChange, 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.

TSX
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 (or defaultChecked) for the starting value, not value.
  • 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?

ControlledUncontrolled
Source of truthReact stateThe DOM
Re-renders while typingYesNo
Instant validation and formattingEasyHarder
Code amountMoreLess
File inputsNot possibleYes

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

TSX
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 checked and e.target.checked, not value.
  • <select> uses value on the select, not selected on options.
  • The generic update function (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

WordMeaning
Controlled inputValue comes from React state
Uncontrolled inputValue is kept in the DOM
defaultValueThe starting value of an uncontrolled input
FormDataReads all named fields of a form
Form actionA 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.