Sub topic 3
Controlled and Uncontrolled Components
Assume you know function components, JSX, props, event handlers, and basic hooks. Reusable components often need to manage state in two ways.
Controlled and uncontrolled components
A controlled component receives its current state through a prop. Its parent updates that prop after receiving an event. An uncontrolled component owns state internally and accepts only an initial value.
import { useState } from 'react';
export function Switch({ on: controlledOn, defaultOn = false, onChange }) {
const controlled = controlledOn !== undefined;
const [internalOn, setInternalOn] = useState(defaultOn);
const on = controlled ? controlledOn : internalOn;
function toggle() {
const next = !on;
if (!controlled) setInternalOn(next);
onChange?.(next);
}
return (
<button role="switch" aria-checked={on} onClick={toggle}>
{on ? 'On' : 'Off'}
</button>
);
}Do not switch between modes during a component's lifetime. That creates two competing sources of truth.
Control props for compound components
A compound component can expose control props while its subcomponents still share context. For example, Tabs can accept value and onValueChange for controlled use, or defaultValue for uncontrolled use. The context should expose the resolved value and a single update function. This keeps Tab and Panel independent of the state-management choice.
Accessibility in compound components
Markup flexibility does not remove accessibility responsibilities. A tabs implementation usually needs:
role="tablist"on the container.role="tab"andaria-selectedon each tab.role="tabpanel"on the visible panel.- Matching IDs through
aria-controlsandaria-labelledby.
Keyboard navigation and focus management may also be required for a production tabs component.
Prop getters and state reducers
A prop getter is a function such as getTogglerProps() that returns the attributes and handlers a consumer should spread onto an element. It combines library behavior with consumer props, rather than hiding the required markup.
A state reducer lets consumers change transitions without replacing the whole component. The component dispatches actions, then passes the current state and action through the supplied reducer. For example, a toggle reducer could ignore attempts to turn an already-on value off.
Headless components and hooks
A headless component or hook provides behavior, state, and accessibility data without imposing visual markup. Hooks offer the most direct composition. Headless components can provide stronger structure while leaving styling to the consumer.
Choosing a pattern
| Pattern | Best when | Main cost |
|---|---|---|
| Controlled component | The parent must coordinate state | More parent code |
| Compound component | Related parts need flexible markup | Context and API complexity |
| Custom hook | Logic should work with many layouts | Consumers handle accessibility |
| Headless component | Behavior is shared but styling varies | More integration work |
Choose the simplest pattern that supports the required reuse. Add reducers, prop getters, or context only when they solve a real extension problem.
Exercise
When should a reusable switch expose defaultOn instead of requiring an on prop?
Show answer
Use defaultOn when the switch can manage its own state and the parent only needs the initial value. Use on when the parent must own and coordinate the current state.