
ReactJS
Form events handle user input and submissions. React makes form handling controlled and predictable.
function LoginForm() {
const [email, setEmail] = React.useState("");
const [password, setPassword] = React.useState("");
const handleSubmit = (e) => {
e.preventDefault();
console.log("Submitting:", 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>
);
}The onChange event fires whenever an input value changes:
function FormField() {
const [text, setText] = React.useState("");
return (
<div>
<input
value={{text}}
onChange={{(e) => setText(e.target.value)}}
placeholder="Type here..."
/>
<p>You typed: {{text}}</p>
</div>
);
}<input type="text" onChange={{handleChange}} />
<textarea onChange={{handleChange}} />
<input type="checkbox" onChange={{handleChange}} />
<select onChange={{handleChange}}>
<option value="option1">Option 1</option>
</select>function ValidatedInput() {
const [email, setEmail] = React.useState("");
const [error, setError] = React.useState("");
const handleChange = (e) => {
const value = e.target.value;
setEmail(value);
if (!value.includes("@")) {
setError("Please enter a valid email");
} else {
setError("");
}
};
return (
<div>
<input value={{email}} onChange={{handleChange}} placeholder="Email" />
{{error && <p style={{{ color: "red" }}}>{{error}}</p>}}
</div>
);
}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