Sub topic 11
Controlled and Uncontrolled Components
This chapter assumes you know function components, props, useState, and passing callbacks as props. It covers how one component can support two ways of owning its state.
Two owners of state
A component is uncontrolled when it keeps its own state and the parent only sets a starting value. A component is controlled when the parent owns the state, passes it down as a prop, and receives change requests through a callback.
React's <input> works both ways. <input defaultValue="a" /> is uncontrolled. <input value={text} onChange={...} /> is controlled. A reusable component can offer the same choice.
| Row | Uncontrolled | Controlled |
|---|---|---|
| State lives in | The component | The parent |
| Initial value prop | `defaultOn` | none, the value is always passed |
| Current value prop | none | `on` |
| Change notification | Optional `onChange` | Required `onChange` to see updates |
A switch that supports both modes
import { useState } from 'react';
export function Switch({ on: controlledOn, defaultOn = false, onChange }) {
const isControlled = controlledOn !== undefined;
const [internalOn, setInternalOn] = useState(defaultOn);
const on = isControlled ? controlledOn : internalOn;
function toggle() {
const next = !on;
if (!isControlled) {
setInternalOn(next);
}
onChange?.(next);
}
return (
<button role="switch" aria-checked={on} onClick={toggle}>
{on ? 'On' : 'Off'}
</button>
);
}The component decides the mode by checking whether on was passed. It always keeps internalOn, but it only reads it when uncontrolled. On a click it computes the next value. It updates its own state only when uncontrolled, and it always calls onChange, so the parent hears about every change.
flowchart TDUsing each mode
import { useState } from 'react';
import { Switch } from './Switch';
export function Demo() {
const [enabled, setEnabled] = useState(false);
return (
<>
<Switch defaultOn={true} />
<Switch on={enabled} onChange={setEnabled} />
<Switch
on={enabled}
onChange={(next) => {
if (window.confirm('Change setting?')) setEnabled(next);
}}
/>
</>
);
}The first switch manages itself. The second and third share one value owned by Demo. The third shows why control is useful: the parent can refuse a change, and the switch stays as it was.
Common errors
- Switching modes during the lifetime of a component. Passing
on={undefined}on one render andon={true}on the next changes the mode. React warns about this for inputs. Decide the mode once and keep it. - Passing
onwithoutonChange. The switch is frozen because nothing updates the prop. - Treating
nullas "not controlled". The check above usesundefined. Anullvalue counts as controlled. - Reading state right after calling a setter. Compute
nextfrom the current value, astoggledoes, and pass that toonChange.
Exercise
A parent renders <Switch on={false} /> with no onChange. What happens when the user clicks the switch, and why?
Show answer
Nothing visible changes. The component is controlled because on is defined, so it does not update its internal state. It only calls onChange?.(next), which does nothing here. The parent never changes on, so the switch stays Off.