React - Controlled Form Components
Forms are a core part of almost every modern web application. From login pages and registration forms to feedback and data-entry systems, handling user input efficiently is critical. In React, form handling is done differently compared to traditional HTML, and one of the most important concepts to understand is Controlled Form Components.
Controlled inputs accept their current value as a prop and a callback to change that value. That implies that the value of the input has to live in the React state somewhere. Typically, the component that renders the input (like a form component) saves that in its state.
1. What Are Controlled Components in React?
A controlled component in React is a form element—such as an input, textarea, select, checkbox, or radio button—whose value is controlled entirely by React state.
In standard HTML, form elements manage their own state internally. React, however, encourages you to store form values in component state and update them using event handlers.
This approach aligns perfectly with React’s unidirectional data flow and component-based architecture.
How it works
- User types in the input field. Every keystroke triggers the onChange event.
- onChange handler updates state . The new input value is stored in React state using setState (or a state setter from useState).
- State change causes a re-rendering. React re-renders the component with the updated state.
- Input value is controlled by state. The input’s value prop is set from state, so what you see in the input always reflects the current state value.
2. Uncontrolled Components
- The input field does not use the value attribute.
- The DOM keeps track of the input’s value.
- useRef() creates a reference to the input element.
- On form submission, inputRef.current.value retrieves the current input value.
3. Difference between Uncontrolled and Controlled Components
|
Feature |
Controlled Component |
Uncontrolled Component |
|
Data stored in |
React state |
DOM |
|
Value accessed via |
‘value’ prop |
‘ref’ |
|
React re-renders |
On every change |
Only when needed |
|
Best for |
Complex forms |
Simple forms |
4. Why Use Controlled Form Components?
5. Handling Multiple Inputs Using a Single State Object
How This Example Works
1. Form State
const [formData, setFormData] = useState({
username: “”,
email: “”,
password: “”
});
- It stores all input values in a single state object
2. Controlled Inputs
<input
type=”text”
name=”username”
value={formData.username}
onChange={handleChange}
placeholder=”Username”
/>
- The value comes from React state
- onChange updates state on every keystroke. This is what makes the form controlled.
3. Single Change Handler
const handleChange = (e) => {
};
- It works for all inputs
- It uses name attribute to update the correct field
4. Field Validation Logic
const validate = () => {
const newErrors = {};
// validation rules
};
- Validation includes : Required field checks, Email format validation, Minimum password length,
- Errors are stored in a separate errors state object.
5. Preventing Page Reload
const handleSubmit = (e) => {
e.preventDefault();
};
- It prevents browser refresh
- This allows React to handle form submission logic