React — Controlled Components
What are Controlled Components?
Controlled components are form elements whose values are controlled by React state. The value is set via state and updated via event handlers.
Basic Controlled Input
function ControlledInput() {
const [value, setValue] = useState('');
return (
<input
type="text"
value={value}
onChange={(e) => setValue(e.target.value)}
/>
);
}
Controlled Textarea
function ControlledTextarea() {
const [value, setValue] = useState('');
return (
<textarea
value={value}
onChange={(e) => setValue(e.target.value)}
/>
);
}
Controlled Select
function ControlledSelect() {
const [value, setValue] = useState('apple');
return (
<select
value={value}
onChange={(e) => setValue(e.target.value)}
>
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="orange">Orange</option>
</select>
);
}
Multiple Controlled Inputs
function ControlledForm() {
const [formData, setFormData] = useState({
firstName: '',
lastName: '',
email: ''
});
const handleChange = (e) => {
const { name, value } = e.target;
setFormData(prev => ({
...prev,
[name]: value
}));
};
return (
<form>
<input
name="firstName"
value={formData.firstName}
onChange={handleChange}
placeholder="First Name"
/>
<input
name="lastName"
value={formData.lastName}
onChange={handleChange}
placeholder="Last Name"
/>
<input
name="email"
type="email"
value={formData.email}
onChange={handleChange}
placeholder="Email"
/>
<p>Hello, {formData.firstName} {formData.lastName}!</p>
</form>
);
}
Controlled Checkbox and Radio
function ControlledCheckbox() {
const [isChecked, setIsChecked] = useState(false);
return (
<label>
<input
type="checkbox"
checked={isChecked}
onChange={(e) => setIsChecked(e.target.checked)}
/>
I agree to the terms
</label>
);
}
function ControlledRadio() {
const [selected, setSelected] = useState('option1');
return (
<div>
<label>
<input
type="radio"
value="option1"
checked={selected === 'option1'}
onChange={(e) => setSelected(e.target.value)}
/>
Option 1
</label>
<label>
<input
type="radio"
value="option2"
checked={selected === 'option2'}
onChange={(e) => setSelected(e.target.value)}
/>
Option 2
</label>
</div>
);
}
Mini Practice
Write React code that:
- Creates a controlled input
- Creates a controlled textarea
- Creates a controlled select
- Uses controlled checkbox and radio
Up Next
Next: Learn about Hooks.
Related Topics
Frequently Asked Questions about Controlled Components
What is Controlled Components in React?
Controlled Components is a fundamental concept in React. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Controlled Components?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Controlled Components.
Why is Controlled Components important in React?
Controlled Components is essential for React development. Understanding this concept will help you write better code and solve real-world problems more effectively.