Controlled Components

Last updated: 2026-11-02

Controlled Components Controlled Components

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.

GroupComponent Patterns
KindPattern

ℹ️ 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.

💡 Key Points

💡 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.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

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.com

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.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏