
ReactJS
Forms are the primary way users input data. Use state to capture and manage form input values.
A controlled component's value comes from state. When the user types, you update state, which updates the input.
The form input's value is controlled by state. This gives React full control over what the user sees and allows you to manipulate the input programmatically.
import { useState } from 'react';
function SimpleForm() {
const [name, setName] = useState('');
const handleSubmit = (e) => {
e.preventDefault();
console.log('Name:', name);
setName(''); // Clear form
};
return (
<form onSubmit={handleSubmit}>
<input
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Enter your name"
/>
<button type="submit">Submit</button>
</form>
);
}<details>
<summary>📚 More Examples</summary>
// Example: Multi-field form
function LoginForm() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const handleSubmit = (e) => {
e.preventDefault();
console.log('Logging in with:', { email, password });
};
return (
<form onSubmit={handleSubmit}>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="Email"
/>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Password"
/>
<button type="submit">Login</button>
</form>
);
}</details>
Different input types need different handling. Text inputs use `value`, checkboxes use `checked`, selects use `value`.
import { useState } from 'react';
function FormTypes() {
const [text, setText] = useState('');
const [checked, setChecked] = useState(false);
const [selected, setSelected] = useState('option1');
const [textarea, setTextarea] = useState('');
return (
<div>
{/* Text input */}
<input
value={text}
onChange={(e) => setText(e.target.value)}
placeholder="Type here"
/>
{/* Checkbox */}
<label>
<input
type="checkbox"
checked={checked}
onChange={(e) => setChecked(e.target.checked)}
/>
Agree to terms
</label>
{/* Select */}
<select value={selected} onChange={(e) => setSelected(e.target.value)}>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
</select>
{/* Textarea */}
<textarea
value={textarea}
onChange={(e) => setTextarea(e.target.value)}
placeholder="Message"
/>
</div>
);
}<details>
<summary>📚 More Examples</summary>
// Example: Radio buttons
function SurveyForm() {
const [rating, setRating] = useState('good');
return (
<div>
<label>
<input
type="radio"
value="bad"
checked={rating === 'bad'}
onChange={(e) => setRating(e.target.value)}
/>
Bad
</label>
<label>
<input
type="radio"
value="good"
checked={rating === 'good'}
onChange={(e) => setRating(e.target.value)}
/>
Good
</label>
<p>You selected: {rating}</p>
</div>
);
}</details>
Prevent the default form submission with `e.preventDefault()` and handle the submission in your event handler.
import { useState } from 'react';
function SignupForm() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [message, setMessage] = useState('');
const handleSubmit = async (e) => {
e.preventDefault();
// Validate
if (!email || !password) {
setMessage('Please fill in all fields');
return;
}
// Submit
try {
const response = await fetch('/api/signup', {
method: 'POST',
body: JSON.stringify({ email, password }),
});
setMessage('Account created!');
setEmail('');
setPassword('');
} catch (error) {
setMessage('Error: ' + error.message);
}
};
return (
<form onSubmit={handleSubmit}>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="Email"
/>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Password"
/>
<button type="submit">Sign Up</button>
{message && <p>{message}</p>}
</form>
);
}| Input Type | State Property | Event Property |
|---|---|---|
| Text input | `value` | `onChange` |
| Checkbox | `checked` | `onChange` |
| Radio button | `checked` | `onChange` |
| Select | `value` | `onChange` |
| Textarea | `value` | `onChange` |
Ready to practice? Challenges | Next: State vs Props
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward