The Standard Controlled Input Pattern
You'll learn the canonical three-part pattern that defines a controlled input in React.
The Three-Part Recipe
Every controlled input follows the same simple pattern. It's like a perfect handshake between three pieces:
- State variable — holds the current value
valueprop — displays that state on the inputonChangehandler — updates the state when the user types
These three always work together. Miss one, and the pattern breaks.
The Pattern in Action
const [name, setName] = useState('');
<input
value={name}
onChange={(e) => setName(e.target.value)}
/>
That's it. This is the standard pattern you'll write hundreds of times.
Why This Pattern Works
Think of it as a loop:
- The input shows whatever's in
name(viavalue) - User types →
onChangefires - Handler reads
e.target.valueand callssetName() - React re-renders, input now shows the new
name
State is the single source of truth. The DOM simply reflects it.
The Universal Template
Every controlled input you create follows this exact structure. Whether it's a text input, textarea, or select dropdown, the pattern stays the same: state variable, value prop, onChange handler calling setState.
You can copy this pattern, swap the state variable name, and you have a working controlled input. It's React's most common pattern for a reason—it's predictable, testable, and gives you complete control over the input's behavior.
Key Takeaway: The standard controlled input pattern is always three parts: a state variable, the value prop set to that state, and an onChange handler that updates the state with e.target.value.