Controlled Components in React Explained

Learn Controlled Components in React with practical examples. Understand form handling, useState, value, onChange, validation, and controlled vs uncontrolled components.
Controlled Components in React: Managing Forms with State
Forms are an essential part of modern web applications.
Examples include:
- Login forms
- Registration forms
- Search bars
- Checkout forms
- Contact forms
When working with forms in React, one of the most common patterns is using Controlled Components .
Unlike Uncontrolled Components, where the DOM manages form values, Controlled Components allow React State to control and manage form data.
This approach provides better control over user input, validation, and dynamic UI updates.
In this guide, you'll learn what Controlled Components are, how they work, and why they are widely used in React applications.
Prerequisites
Before learning Controlled Components, make sure you understand:
These concepts will help you understand form management in React.
What Are Controlled Components?
A Controlled Component is a form element whose value is controlled by React State.
React becomes the single source of truth for the input value.
Example:
import { useState } from "react";
function LoginForm() {
const [name, setName] =
useState("");
return (
<input
type="text"
value={name}
onChange={(e) =>
setName(e.target.value)
}
/>
);
}
The value displayed inside the input always comes from React State.
Why Are They Called Controlled Components?
Consider this example:
const [name, setName] =
useState("");
Input:
<input
value={name}
onChange={(e) =>
setName(e.target.value)
}
/>
Flow:
User Types
↓
onChange Event
↓
State Updates
↓
Component Re-Renders
↓
Input Updates
React controls the value completely.
Why Use Controlled Components?
Controlled Components provide:
- real-time validation
- dynamic UI updates
- predictable state management
- easier debugging
- form synchronization
This makes them ideal for most React forms.
Understanding value and onChange
Controlled Components rely on two key properties.
value
value={name}
This tells React what should appear in the input.
onChange
onChange={(e) =>
setName(e.target.value)
}
This updates State whenever the user types.
Both are required for a controlled input.
Simple Example
import { useState } from "react";
function App() {
const [username, setUsername] =
useState("");
return (
<>
<input
value={username}
onChange={(e) =>
setUsername(
e.target.value
)
}
/>
<h2>
{username}
</h2>
</>
);
}
As the user types, the UI updates instantly.
Handling Multiple Inputs
Example:
import { useState } from "react";
function Form() {
const [formData, setFormData] =
useState({
name: "",
email: ""
});
function handleChange(e) {
setFormData({
...formData,
[e.target.name]:
e.target.value
});
}
return (
<>
<input
name="name"
value={formData.name}
onChange={handleChange}
/>
<input
name="email"
value={formData.email}
onChange={handleChange}
/>
</>
);
}
This pattern is common in production applications.
Form Submission Example
function LoginForm() {
const [email, setEmail] =
useState("");
function handleSubmit(e) {
e.preventDefault();
console.log(email);
}
return (
<form
onSubmit={handleSubmit}
>
<input
value={email}
onChange={(e) =>
setEmail(
e.target.value
)
}
/>
<button>
Submit
</button>
</form>
);
}
The form value remains fully controlled by React.
Real-Time Validation
One major advantage of controlled components is validation.
Example:
const [email, setEmail] =
useState("");
const isValid =
email.includes("@");
Display message:
{
!isValid &&
email &&
(
<p>
Invalid Email
</p>
)
}
Validation becomes straightforward because React always knows the current value.
Controlled Select Element
Example:
const [country, setCountry] =
useState("");
<select
value={country}
onChange={(e) =>
setCountry(
e.target.value
)
}
>
<option value="">
Select Country
</option>
<option value="India">
India
</option>
</select>
Controlled Checkbox
Example:
const [accepted, setAccepted] =
useState(false);
<input
type="checkbox"
checked={accepted}
onChange={(e) =>
setAccepted(
e.target.checked
)
}
/>
Checkboxes use checked instead of value .
Behind the Scenes
Controlled Components follow this flow:
Input Change
↓
onChange Event
↓
React State Update
↓
Component Re-Render
↓
Updated UI
React always remains the source of truth.
Real Project Usage
Authentication Forms
Controlled Components manage:
- password
- validation messages
E-commerce Checkout
Controlled Components manage:
- billing details
- shipping address
- payment information
Search Systems
Controlled Components manage:
- search keywords
- live filtering
- autocomplete suggestions
Common Real-World Use Cases
Controlled Components are commonly used for:
- login forms
- registration forms
- search bars
- checkout forms
- filters
- profile settings
- dashboards
These use cases often require validation and real-time updates.
Controlled vs Uncontrolled Components
| Feature | Controlled | Uncontrolled |
|---|---|---|
| Data Stored In | React State | DOM |
| Uses useState | Yes | No |
| Uses useRef | Optional | Usually |
| Validation | Easy | Difficult |
| Re-Renders | More | Fewer |
| UI Synchronization | Excellent | Limited |
Both approaches are useful depending on the situation.
When NOT to Use Controlled Components
Avoid controlled components when:
- handling very large forms with performance concerns
- integrating third-party DOM libraries
- building simple file upload inputs
Example:
<input
type="file"
/>
File inputs are usually handled as uncontrolled components.
Common Beginner Mistakes
Forgetting onChange
Incorrect:
<input
value={name}
/>
This creates a read-only input.
Always provide:
onChange={handleChange}
Mutating Form State
Incorrect:
formData.name =
"Sachin";
Always use:
setFormData(...)
Creating Separate Handlers for Every Input
For large forms, reusable handlers are often better.
Overusing State
Not every field requires complex state structures.
Keep form logic simple.
Controlled Components Interview Questions
- What is a Controlled Component in React?
- Why is React State called the source of truth?
- What is the purpose of the value prop?
- What is the purpose of onChange?
- What are the advantages of Controlled Components?
- What is the difference between Controlled and Uncontrolled Components?
- Why are file inputs usually uncontrolled?
Watch Full Controlled Components Tutorial
If you prefer video learning, watch the complete tutorial below where we build forms, validations, and real-world examples using Controlled Components.
Watch the Full Controlled Components Tutorial
This tutorial demonstrates how professional React applications manage form data.
Build Something
Practice Controlled Components by creating:
- Login Form
- Registration Form
- Contact Form
- Search Filter
- Checkout Form
These projects will strengthen your understanding of React form handling.
Production Tip
Professional React developers usually:
- use controlled components for validation-heavy forms
- centralize form state
- create reusable input components
- avoid unnecessary state complexity
- use form libraries for very large forms
Controlled Components provide excellent control and predictability for most React applications.
Why Controlled Components Matter
Controlled Components allow React to manage form data completely.
This makes validation, dynamic UI updates, and state synchronization significantly easier.
For this reason, controlled components are the most commonly used approach for handling forms in React.
Conclusion
Controlled Components are form elements whose values are managed by React State.
By combining value and onChange , developers can create predictable, validated, and highly interactive forms.
Understanding Controlled Components is essential for building modern React applications and handling user input effectively.