React — Forms
Basic Form
function Form() {
const handleSubmit = (e) => {
e.preventDefault();
console.log('Form submitted');
};
return (
<form onSubmit={handleSubmit}>
<input type="text" />
<button type="submit">Submit</button>
</form>
);
}
Controlled Inputs
function LoginForm() {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const handleSubmit = (e) => {
e.preventDefault();
console.log('Login:', { username, password });
};
return (
<form onSubmit={handleSubmit}>
<input
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<button type="submit">Login</button>
</form>
);
}
Textarea
function MessageForm() {
const [message, setMessage] = useState('');
return (
<form>
<textarea
value={message}
onChange={(e) => setMessage(e.target.value)}
/>
<p>Message length: {message.length}</p>
</form>
);
}
Select
function ColorPicker() {
const [color, setColor] = useState('red');
return (
<select
value={color}
onChange={(e) => setColor(e.target.value)}
>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
</select>
);
}
Form Examples
// Registration form
function RegistrationForm() {
const [formData, setFormData] = useState({
name: '',
email: '',
password: '',
confirmPassword: ''
});
const handleChange = (e) => {
setFormData({
...formData,
[e.target.name]: e.target.value
});
};
const handleSubmit = (e) => {
e.preventDefault();
if (formData.password !== formData.confirmPassword) {
alert('Passwords do not match');
return;
}
console.log('Register:', formData);
};
return (
<form onSubmit={handleSubmit}>
<input
name="name"
value={formData.name}
onChange={handleChange}
placeholder="Name"
/>
<input
name="email"
type="email"
value={formData.email}
onChange={handleChange}
placeholder="Email"
/>
<input
name="password"
type="password"
value={formData.password}
onChange={handleChange}
placeholder="Password"
/>
<input
name="confirmPassword"
type="password"
value={formData.confirmPassword}
onChange={handleChange}
placeholder="Confirm Password"
/>
<button type="submit">Register</button>
</form>
);
}
// Search form
function SearchForm() {
const [query, setQuery] = useState('');
const handleSearch = (e) => {
e.preventDefault();
console.log('Search:', query);
};
return (
<form onSubmit={handleSearch}>
<input
type="search"
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder="Search..."
/>
<button type="submit">Search</button>
</form>
);
}
Mini Practice
Write React code that:
- Creates a basic form
- Uses controlled inputs
- Handles form submission
- Creates a multi-field form
Up Next
Next: Learn about Controlled Components.
Related Topics
Frequently Asked Questions about Forms
What is Forms in React?
Forms 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 Forms?
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 Forms.
Why is Forms important in React?
Forms is essential for React development. Understanding this concept will help you write better code and solve real-world problems more effectively.