Controlled components let you keep form data inside React state. This helps you manage and use the data easily.
Controlled components in React
Introduction
Syntax
React
function MyComponent() { const [value, setValue] = React.useState(''); function handleChange(event) { setValue(event.target.value); } return ( <input type="text" value={value} onChange={handleChange} /> ); }
The value attribute sets the input's current value from React state.
The onChange event updates the state when the user types.
Examples
React
const [name, setName] = React.useState(''); <input value={name} onChange={e => setName(e.target.value)} />
checked attribute.React
const [checked, setChecked] = React.useState(false); <input type="checkbox" checked={checked} onChange={e => setChecked(e.target.checked)} />
React
const [text, setText] = React.useState('Hello'); <textarea value={text} onChange={e => setText(e.target.value)} />
Sample Program
This component shows a text input controlled by React state. As you type, the text below updates instantly.
React
import React from 'react'; export default function ControlledInput() { const [inputValue, setInputValue] = React.useState(''); function handleChange(event) { setInputValue(event.target.value); } return ( <div> <label htmlFor="nameInput">Name: </label> <input id="nameInput" type="text" value={inputValue} onChange={handleChange} aria-label="Name input" /> <p>You typed: {inputValue}</p> </div> ); }
Important Notes
Always set the value or checked attribute to keep input controlled.
Without onChange, the input will be read-only.
Use aria-label or label for accessibility.
Summary
Controlled components keep form data in React state.
Use value and onChange to sync input and state.
This helps with validation, formatting, and dynamic UI updates.
Practice
1. What is a controlled component in React?
easy
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]
Hint: Controlled means React state holds the input value [OK]
Common Mistakes:
- Confusing controlled with uncontrolled components
- Thinking refs control input value
- Assuming controlled means no state usage
2. Which of the following is the correct way to create a controlled input in React?
easy
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]
Hint: Controlled input needs value and onChange props [OK]
Common Mistakes:
- Using defaultValue instead of value for controlled inputs
- Omitting onChange handler
- Not linking value to state
3. What will be the output when typing in this controlled input?
function MyInput() {
const [text, setText] = React.useState('');
return (
<input value={text} onChange={e => setText(e.target.value)} />
);
}medium
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]
Hint: State + onChange updates input value dynamically [OK]
Common Mistakes:
- Thinking input won't update without defaultValue
- Assuming input is uncontrolled without onChange
- Confusing initial state with input behavior
4. Identify the error in this controlled component code:
function Form() {
const [name, setName] = React.useState('');
return (
<input value={name} onChange={e => setName} />
);
}medium
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]
Hint: onChange must call setState with event target value [OK]
Common Mistakes:
- Passing setName without calling it
- Using defaultValue instead of value in controlled input
- Forgetting to pass initial state to useState
5. You want to create a controlled textarea that converts all input to uppercase as the user types. Which code snippet correctly implements this?
hard
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]
Hint: Transform input in onChange before setting state [OK]
Common Mistakes:
- Transforming value prop instead of input value
- Using defaultValue in controlled component
- Not updating state with transformed input
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
