Forms and controlled inputs

Beginner · 9 min

Controlled vs. uncontrolled inputs, wiring value and onChange, handling several form fields with one shared handler, and where React 19 form actions fit.

Before thisuseState and component stateHandling events in React

Net Ninja · 9 min

Forms and controlled inputs

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Net NinjaReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • An uncontrolled input manages its own value internally in the DOM, the way a plain HTML `<input>` always has; React never sees keystrokes unless you read the element directly.
  • A controlled input's value is driven entirely by React state: the `value` prop sets what's displayed, and an `onChange` handler updates that state on every keystroke, so the input only ever shows what state says it should.
  • A controlled input without an `onChange` handler freezes — the DOM tries to update, React re-renders with the old `value` prop, and the input snaps back, visually blocking further typing.
  • Several fields can share one `handleChange` function by reading `event.target.name` and `event.target.value`, then updating a single state object with that one key, instead of writing a separate handler per field.
  • A shared handler needs each input's `name` attribute to match the corresponding key in the state object, since the handler uses `name` to know which key to update.
  • React 19 added native form actions — passing a function straight to a form's `action` prop — as an alternative to controlled inputs for simple submission flows; this lesson stays with the classic value/onChange pattern, and actions get full treatment later in this roadmap.

Who owns the value: the DOM, or React?

A plain HTML <input> is uncontrolled by nature — type into it, and the browser’s own DOM updates the value, with no framework involved at all:

function UncontrolledInput() {
  return <input type="text" />;
}

React can still read this input’s current value when it needs to (typically with a ref, covered later in this roadmap), but nothing about typing into it touches React state. The input is “uncontrolled” because React doesn’t control what it displays.

A controlled input flips that: React state becomes the single source of truth for what the input shows, and the input is wired to follow it.

function ControlledInput() {
  const [text, setText] = useState("");

  return (
    <input
      type="text"
      value={text}
      onChange={(event) => setText(event.target.value)}
    />
  );
}

value={text} tells the DOM exactly what to display: whatever text currently is, full stop. onChange fires on every keystroke, reads the new value out of event.target.value, and feeds it back into state via setText. That re-render then feeds the new value straight back into value={text}, so the loop completes and the input shows what was just typed. The data flows in one direction, state to input, every time.

The frozen-input bug

Leave out the onChange handler and keep value={text}, and typing appears to do nothing:

// Missing onChange — don't do this
<input type="text" value={text} />

The keystroke briefly changes what the DOM element is about to show, but React immediately re-renders and reasserts value={text}, which never changed, so the display snaps back to the old value. The input looks broken rather than obviously wrong, which makes this a common and confusing beginner mistake. Any controlled input needs both halves — value to display state, and onChange to keep that state in sync with what the user does.

One handler, many fields

A form with several fields doesn’t need a separate useState and handler per field. Give each input a name attribute, store all the values in one state object, and let a single handler update whichever key matches the input that fired it:

function SignupForm() {
  const [form, setForm] = useState({ email: "", password: "" });

  function handleChange(event) {
    const { name, value } = event.target;
    setForm((prev) => ({ ...prev, [name]: value }));
  }

  return (
    <form>
      <input name="email" value={form.email} onChange={handleChange} />
      <input name="password" type="password" value={form.password} onChange={handleChange} />
    </form>
  );
}

event.target.name tells the handler which input fired the event, and the computed property [name] uses that string to update just the matching key in form, spreading the rest of the object unchanged. This relies on the functional-update form from the previous lesson, since the new object depends on the previous one. It also depends on each input’s name lining up exactly with a key in the state object — a typo in name means the handler silently updates (or creates) the wrong key.

A note on React 19 form actions

React 19 introduced form actions: a function passed directly to a form’s action prop, which React calls with the form’s data when it’s submitted, without you wiring up value/onChange on every field yourself. It’s a real alternative to the controlled-input pattern above, particularly for simple submit-and-send flows, and it plays well with pending-state and error-handling APIs introduced alongside it. This lesson deliberately doesn’t go further into actions — controlled inputs are still the pattern you’ll reach for whenever you need to validate, transform, or react to input on every keystroke rather than only on submit — and the full treatment of actions is a dedicated lesson later in this roadmap, server actions and mutations.

What the video covers

Net Ninja’s episode gives a clear, focused walkthrough of controlled vs. uncontrolled inputs and the value/onChange pattern with a single input field, from a hooks-era React codebase whose syntax is still current. It doesn’t extend the example to multiple fields sharing one handler, which the section above and the linked Code Bless You video cover, and it naturally predates React 19’s form actions, which this lesson’s closing section flags on its own.

Where this fits

Forms are the first place where getting the rules around hooks exactly right starts to matter in practice — covered next in the rules of hooks.

Check yourself

  1. 01What makes an input "controlled" rather than "uncontrolled" in React?

    A controlled input's displayed value comes entirely from React state via the `value` prop, with an `onChange` handler updating that state on every change. An uncontrolled input manages its value itself in the DOM, the way a plain HTML input does, with React only reading it on demand (for example via a ref) rather than driving it.

  2. 02You set value={text} on an input but forget to add an onChange handler. What happens when the user tries to type?

    The DOM briefly updates to show the typed character, but React re-renders right after and reapplies value={text}, which is still the old, unchanged state. The input visually reverts and effectively refuses to accept input, since nothing is updating the state that value is bound to.

  3. 03How can one handleChange function update three different text inputs without three separate handlers?

    Give each input a name attribute matching a key in one state object, then in handleChange read event.target.name and event.target.value to update just that key, typically with something like setForm(prev => ({ ...prev, [event.target.name]: event.target.value })). The single function works for any input whose name matches a key in that object.

  4. 04What did React 19 add as an alternative way to handle form submission, and does this lesson cover it in depth?

    React 19 added native form actions, where a function is passed directly to a form's action prop instead of (or alongside) controlled inputs and a submit handler. This lesson only flags that it exists; the deep treatment is a later lesson, server actions and mutations.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark