π React Mastery Series β Day 18

Frontend Developer | React & JavaScript | Building production-ready web apps Developer with 1.5+ years of experience, currently focused on React, Redux, and full-stack development. Sharing practical tutorials, concepts, and project learnings.
Controlled vs Uncontrolled Components β Forms Mastery in React
Forms are everywhere:
Login pages
Signup forms
Search inputs
Checkout pages
But in React, forms work differently than plain HTML.
Today we understand:
π Controlled Components
π Uncontrolled Components
π§ 1οΈβ£ Controlled Components
π Definition
A Controlled Component is a form element whose value is controlled by React state.
React controls the input.
π Example β Controlled Input
import { useState } from "react";
function ControlledForm() {
const [name, setName] = useState("");
const handleSubmit = (e) => {
e.preventDefault();
alert(`Submitted Name: ${name}`);
};
return (
<form onSubmit={handleSubmit}>
<input
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Enter your name"
/>
<button type="submit">Submit</button>
</form>
);
}
π Whatβs happening?
Input value comes from state
onChange updates state
React fully controls the input
Thatβs why it's called Controlled.
β Advantages of Controlled Components
β Easy validation
β Real-time error handling
β Better control over form data
β Predictable behavior
Most real-world apps use this pattern.
π§ 2οΈβ£ Uncontrolled Components
π Definition
An Uncontrolled Component stores its own internal state in the DOM.
React does NOT control the input directly.
We use useRef to access its value.
π Example β Uncontrolled Input
import { useRef } from "react";
function UncontrolledForm() {
const inputRef = useRef();
const handleSubmit = (e) => {
e.preventDefault();
alert(`Submitted Name: ${inputRef.current.value}`);
};
return (
<form onSubmit={handleSubmit}>
<input type="text" ref={inputRef} placeholder="Enter your name" />
<button type="submit">Submit</button>
</form>
);
}
Here:
Value is stored in DOM
React reads it using ref
No state management
π₯ Key Difference
| Controlled | Uncontrolled |
| Managed by React state | Managed by DOM |
| Uses useState | Uses useRef |
| More predictable | Less control |
| Best for complex forms | Simple quick forms |
π― When to Use What?
β Use Controlled for:
Login / Signup forms
Validation-heavy forms
Dynamic form behavior
β Use Uncontrolled for:
Simple input fields
Quick integrations
When you donβt need validation
π§ Interview One-Liner
Controlled components are managed by React state, while uncontrolled components rely on the DOM to manage their own state.
π Real-World Insight
In professional projects:
π 90% of the time you will use Controlled Components.
Because validation and dynamic UI are very common.
β Key Takeaways
β Controlled = React controls input
β Uncontrolled = DOM controls input
β Controlled is more powerful
β Uncontrolled is simpler
π Coming Next
Day 19 β Lifting State Up (Component Communication Mastery)
Now we level up communication between components π₯




