Controlled vs. uncontrolled components

Intermediate · 9 min

Who owns a component's state — itself or its parent — generalized beyond form inputs, and how to design an API that supports both.

Before thisForms and controlled inputsuseRef and mutable values

Code Ryan · 9 min

Controlled vs. uncontrolled components

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

Taught by Code RyanReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Controlled vs. uncontrolled is a question of ownership: does the component hold its own state internally, or does a parent own that state and pass it down, with the component just reporting changes back up.
  • This split was introduced for form inputs, but the same question applies to any stateful component — an Accordion, a Tabs widget, a modal's open state — not just <input> and <textarea>.
  • A controlled component receives its value as a prop and has no internal state for it; the parent decides what to render and must update that prop in response to the component's change events.
  • An uncontrolled component manages that same value with its own internal state (or a ref), and a parent that wants to know about a change listens to a callback rather than owning the value.
  • A well-designed custom component supports both: it accepts optional value and onChange props, and only falls back to internal state when the parent doesn't supply value.
  • A component accidentally mixing the two — reading a parent-supplied value prop but also keeping its own state for it — tends to fall out of sync, which is why React warns about switching an input between controlled and uncontrolled across renders.

The ownership question, stated plainly

A controlled input looks like this:

function SearchBox({ value, onChange }) {
  return <input value={value} onChange={(event) => onChange(event.target.value)} />;
}

function App() {
  const [query, setQuery] = useState("");
  return <SearchBox value={query} onChange={setQuery} />;
}

App owns query. SearchBox never decides what text is in the box on its own; it only reports keystrokes upward and renders exactly whatever App tells it to. An uncontrolled version flips that:

function SearchBox({ onSubmit }) {
  const inputRef = useRef(null);
  function handleSubmit() {
    onSubmit(inputRef.current.value);
  }
  return (
    <>
      <input ref={inputRef} defaultValue="" />
      <button onClick={handleSubmit}>Search</button>
    </>
  );
}

Now SearchBox owns the text itself, via the DOM and a ref, and only hands a value up when asked. The parent never dictates what’s currently typed.

The same question, away from forms

This is not a fact about <input> specifically — it’s a fact about where state lives, and the exact same question comes up for components that have nothing to do with text entry. Take an Accordion. Does the accordion component decide internally which panel is expanded, or does its parent own that and pass it down?

An uncontrolled accordion owns its own expanded state:

function Accordion({ items }) {
  const [openId, setOpenId] = useState(null);
  return items.map((item) => (
    <section key={item.id}>
      <button onClick={() => setOpenId(item.id === openId ? null : item.id)}>
        {item.title}
      </button>
      {item.id === openId && <div>{item.content}</div>}
    </section>
  ));
}

That’s fine until some page needs to open a specific panel from outside — say, deep-linking to a question in an FAQ. For that, the parent needs to own openId, which means the accordion needs to become controllable.

Designing a component that supports both

The common, flexible answer is to accept value and onChange as optional props, and fall back to internal state only when the parent doesn’t supply them:

function Accordion({ items, openId: controlledOpenId, onOpenChange }) {
  const [internalOpenId, setInternalOpenId] = useState(null);
  const isControlled = controlledOpenId !== undefined;
  const openId = isControlled ? controlledOpenId : internalOpenId;

  function handleToggle(id) {
    const nextId = id === openId ? null : id;
    if (!isControlled) setInternalOpenId(nextId);
    onOpenChange?.(nextId);
  }

  return items.map((item) => (
    <section key={item.id}>
      <button onClick={() => handleToggle(item.id)}>{item.title}</button>
      {item.id === openId && <div>{item.content}</div>}
    </section>
  ));
}

Used with no props beyond items, Accordion manages its own state exactly as before — fully uncontrolled. A parent that cares can pass openId and onOpenChange, taking over ownership without Accordion’s internals changing at all:

<Accordion items={faqItems} openId={openFaqId} onOpenChange={setOpenFaqId} />

This is precisely the pattern <input> itself follows, which is why it’s worth internalizing as a general design tool rather than form-input trivia: check whether a prop meant to own the value is undefined, and switch between reading internalOpenId and the parent-supplied value based on that.

The trap: partial ownership

The one thing not to do is read a value-like prop for rendering while also maintaining internal state for the same thing, updating both independently. The two copies drift apart, and whichever one actually drives the UI becomes a bug waiting to be found. React’s own warning about switching an <input> between controlled and uncontrolled across renders exists precisely because that ambiguity is dangerous: pick one owner per render, consistently, which is exactly what checking controlledOpenId !== undefined on every render does.

What the video covers

Code Ryan’s Controlled vs Uncontrolled Components in React frames the distinction explicitly as who owns the value — React state versus the DOM via a ref — and works through form-input examples with their real trade-offs, which maps directly onto this lesson’s SearchBox example. Its examples stay within forms; generalizing the same ownership question to a non-form component like the Accordion above, and the value/onChange-with-fallback API pattern that supports both modes, is this lesson’s own addition on top of the video.

Where this fits

Ownership of state is also the question behind rendering a subtree somewhere else in the DOM entirely, which is where portals come in. Next: portals.

Check yourself

  1. 01In the controlled vs. uncontrolled split, what exactly is being decided?

    Which side owns the piece of state: the component itself, managing it internally, or the parent that renders the component, holding the value and passing it down as a prop.

  2. 02Is the controlled/uncontrolled distinction specific to form inputs?

    No. It started there, but it applies to any component with internal state a parent might want to own instead, such as which accordion panel is expanded or whether a modal is open.

  3. 03How can a custom component's API support both controlled and uncontrolled usage?

    By accepting optional value and onChange props. When the parent supplies value, the component treats the parent as the owner and renders exactly what it was given. When value is omitted, the component falls back to managing the value itself with internal state.

  4. 04What problem does React warn about regarding controlled and uncontrolled inputs?

    Switching an input between controlled and uncontrolled across renders, typically by passing value sometimes and undefined other times, which causes React to warn because the component cannot consistently tell which side owns the value.

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