What if your form inputs could always stay perfectly in sync with your app data without extra hassle?
Why Controlled components in React? - Purpose & Use Cases
Imagine building a form where you manually read input values from the page every time the user types something, then update the display or state yourself.
Manually syncing input values with your app's data is slow, confusing, and easy to mess up. You might miss updates or show wrong info because the input and data get out of sync.
Controlled components let React manage the input's value in state, so the input always shows the current data and updates happen smoothly and predictably.
const input = document.querySelector('input'); input.addEventListener('input', () => { console.log(input.value); });
const [value, setValue] = React.useState('');
<input value={value} onChange={e => setValue(e.target.value)} />This makes your forms reliable and easy to control, letting you respond instantly to user input and keep your app data accurate.
Think of a signup form where you want to show live validation messages as the user types their email or password.
Manual input handling is error-prone and hard to maintain.
Controlled components keep input values in React state for easy syncing.
This leads to smoother, more predictable user interactions.
Practice
Solution
Step 1: Understand controlled components
Controlled components are form inputs whose values are managed by React state, not by the DOM alone.Step 2: Identify the correct definition
A form input element whose value is controlled by React state correctly describes this concept as React controlling the input value via state.Final Answer:
A form input element whose value is controlled by React state -> Option CQuick Check:
Controlled component = React state controls input value [OK]
- Confusing controlled with uncontrolled components
- Thinking refs control input value
- Assuming controlled means no state usage
Solution
Step 1: Check controlled input syntax
A controlled input must have its value set from state and update state on change.Step 2: Analyze options
<input value={this.state.text} onChange={this.handleChange} /> uses value from state and onChange handler to update it, which is correct.Final Answer:
<input value={this.state.text} onChange={this.handleChange} /> -> Option AQuick Check:
Value + onChange = controlled input [OK]
- Using defaultValue instead of value for controlled inputs
- Omitting onChange handler
- Not linking value to state
function MyInput() {
const [text, setText] = React.useState('');
return (
<input value={text} onChange={e => setText(e.target.value)} />
);
}Solution
Step 1: Understand the controlled input behavior
The input's value is linked to state variable 'text'. The onChange updates 'text' with user input.Step 2: Predict the input behavior
As user types, onChange updates state, which updates value, so input shows typed characters.Final Answer:
The input updates and shows what the user types -> Option AQuick Check:
State updates input value = visible typing [OK]
- Thinking input won't update without defaultValue
- Assuming input is uncontrolled without onChange
- Confusing initial state with input behavior
function Form() {
const [name, setName] = React.useState('');
return (
<input value={name} onChange={e => setName} />
);
}Solution
Step 1: Check onChange handler usage
The onChange handler is set to 'e => setName' which is a function reference, not a call with the new value.Step 2: Correct onChange handler
It should be 'e => setName(e.target.value)' to update state with input value.Final Answer:
The onChange handler does not call setName with the new value -> Option DQuick Check:
onChange must call setState with event value [OK]
- Passing setName without calling it
- Using defaultValue instead of value in controlled input
- Forgetting to pass initial state to useState
Solution
Step 1: Understand controlled textarea with uppercase transformation
The textarea's value must come from state, and onChange must update state with uppercase input.Step 2: Analyze each option
const [text, setText] = React.useState(''); return <textarea value={text} onChange={e => setText(e.target.value.toUpperCase())} />; updates state with uppercase input on change and sets value from state, so textarea shows uppercase as typed.Final Answer:
const [text, setText] = React.useState(''); return <textarea value={text} onChange={e => setText(e.target.value.toUpperCase())} />; -> Option BQuick Check:
Update state with uppercase input onChange = uppercase textarea [OK]
- Transforming value prop instead of input value
- Using defaultValue in controlled component
- Not updating state with transformed input
Ready to prove it?
Pass the test and this pattern moves unknown
