A controlled component is one whose value is driven by React state: the value is bound to state, and onChange writes the new value back into state, closing the loop of one-way data flow plus event feedback. It is the default pattern for React forms, and it keeps the current input continuously validatable, submittable and able to drive other fields without touching the DOM imperatively. Because the state is the single source of truth, a controlled field can be reset, pre-filled or cross-checked against its neighbours at any moment.
Group
Component Patterns
Kind
Pattern
ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.
📝 Syntax
<input
value={name} // state drives the display
onChange={e => setName(e.target.value)} // typing writes back to state
/>
Returns:No return value; this entry describes an encoding pattern rather than a function.
▶ Example
The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.
Output:
After typing "Alice" and picking "Advanced", the line below immediately shows "Hello, Alice (Advanced)" — every keystroke updates state once and the UI re-renders in sync.
💡 Tip value and onChange must appear as a pair: binding value without onChange locks the input and makes it impossible to type.
ℹ️ Info Once controlled, every keystroke lands in state, so validation, disabled buttons and linked dropdowns all become pure functional derivations of state.
⚠️ Warning Once a value is controlled, never let it become undefined, otherwise React complains about switching between controlled and uncontrolled.
Q How do I choose between controlled and uncontrolled components?
A Use controlled when you need instant validation, linked fields, conditional disabling or pre-submit processing; use uncontrolled (reading the value through a ref) when you only care about the value at submit time. React forms default to controlled.
Q What happens if I write value but no onChange?
A The input becomes read-only: React pins the value to the state value, keystrokes have no effect, and the console logs a warning.
Q Does every input need its own useState?
A With many fields you can merge them into one object state (updated by field name), or extract a custom hook / useReducer to manage them in one place.
Web-Tutorial Tech Team
A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.