⚛️ Introduction
Forms are one of the most essential parts of any web application — from login pages to contact forms and checkout screens.
But managing form inputs, tracking user data, and validating it efficiently can be tricky. 😅
Luckily, React provides simple yet powerful tools to handle forms and validation in a clean, declarative way.
In this article, we’ll explore:
✅ How to handle form inputs in React
✅ Controlled vs Uncontrolled Components
✅ Validation techniques (manual & library-based)
✅ Practical examples and best practices
Let’s dive in! 🚀
🧾 1. Forms in React — The Basics
In traditional HTML, form elements like <input>, <textarea>, and <select> maintain their own internal state.
But in React, state should be controlled by components — that’s where controlled components come in.
🎛️ 2. Controlled Components
A controlled component means the form data is handled by the React component’s state instead of the DOM.
Example 👇
import React, { useState } from "react";
function SimpleForm() {
const [name, setName] = useState("");
const handleSubmit = (e) => {
e.preventDefault();
alert(`Hello, ${name}!`);
};
return (
<form onSubmit={handleSubmit}>
<label>Enter your name:</label><br />
<input
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
/>
<button type="submit">Submit</button>
</form>
);
}🧠 How It Works
The value of the input is linked to React’s state (name).
Every keystroke triggers onChange, updating the state.
When submitted, you can access name directly from the component.
✅ Benefits
Easy to manage and validate data.
Keeps form values in sync with React state.
🪄 3. Uncontrolled Components
In uncontrolled components, form data is handled by the DOM itself — not React state.
You access values using refs.
Example 👇
import React, { useRef } from "react";
function UncontrolledForm() {
const inputRef = useRef();
const handleSubmit = (e) => {
e.preventDefault();
alert(`Hello, ${inputRef.current.value}!`);
};
return (
<form onSubmit={handleSubmit}>
<input type="text" ref={inputRef} />
<button type="submit">Submit</button>
</form>
);
}
✅ Simpler for quick forms
⚠️ But harder to validate and debug — that’s why most React apps use controlled components.
✅ 4. Handling Multiple Inputs
If you have several fields, you can manage them in one state object.
Here’s an example with name and email 👇
import React, { useState } from "react";
function MultiInputForm() {
const [formData, setFormData] = useState({
name: "",
email: "",
});
const handleChange = (e) => {
const { name, value } = e.target;
setFormData((prev) => ({
...prev,
[name]: value,
}));
};
const handleSubmit = (e) => {
e.preventDefault();
console.log(formData);
};
return (
<form onSubmit={handleSubmit}>
<label>Name:</label>
<input name="name" value={formData.name} onChange={handleChange} />
<br />
<label>Email:</label>
<input name="email" value={formData.email} onChange={handleChange} />
<br />
<button type="submit">Submit</button>
</form>
);
}

Join the conversation! Your thoughts help the community grow.