The Basic Pattern: useState + value + onChange
What You'll Learn
You'll learn how to wire together React state, the value prop, and the onChange handler to create a fully controlled input that stays perfectly synchronized with your component's state.
The Three-Part Dance
A controlled input in React requires three pieces working together:
- useState — Store the input's current value in state
- value — Bind the input's displayed value to that state
- onChange — Update the state whenever the user types
Think of it like a mirror reflection: the input shows what's in state, and every keystroke updates that state, which updates the display, creating a continuous loop of synchronization.
The Pattern in Code
function NameForm() {
const [name, setName] = useState('');
return (
<input
value={name}
onChange={(e) => setName(e.target.value)}
/>
);
}
Here's what happens:
useState('')creates state starting with an empty stringvalue={name}forces the input to display whatevernamecontainsonChange={(e) => setName(e.target.value)}updatesnameon every keystroke
The input is now "controlled" because React state is the single source of truth. The input can't show anything except what's in state.
Why All Three Matter
Miss one piece and it breaks:
- No
valueprop? The input becomes uncontrolled - No
onChange? The input becomes read-only - No state? You have nothing to bind to
Together, they create React's controlled component pattern: state flows down through value, changes flow up through onChange, and everything stays in perfect sync.
Key Takeaway: The controlled input pattern combines useState for storage, value to display state, and onChange to capture updates—creating a single source of truth managed entirely by React.