Form Events in React
Learn how to handle form inputs, track user-entered values, submit forms, and display results using React event handlers.
1. What Are Form Events?
Form events occur when users interact with form elements. For example, users can type in an input field, select an option, or submit a form.
Common React form events include:
onChange— handles changes to input values.onSubmit— handles form submission.onFocus— handles when an input receives focus.onBlur— handles when an input loses focus.
function SimpleForm() {
function handleSubmit(event) {
event.preventDefault();
alert("Form submitted!");
}
return (
<form onSubmit={handleSubmit}>
<input
type="text"
placeholder="Enter your name"
required
/>
<button type="submit">
Submit
</button>
</form>
);
}
export default SimpleForm;
Explanation: When the user submits
the form, React calls handleSubmit().
preventDefault() stops the browser's
normal form submission and page reload.
2. Handling Input with onChange
The onChange event is used to handle changes
in an input field. In React, we can store the entered value
in state and display it on the page.
import { useState } from "react";
function NameInput() {
const [name, setName] = useState("");
function handleChange(event) {
setName(event.target.value);
}
return (
<div>
<input
type="text"
value={name}
onChange={handleChange}
placeholder="Enter your name"
/>
<p>Your name: {name}</p>
</div>
);
}
export default NameInput;
useState("")creates an empty name value.onChangecallshandleChange().event.target.valuereads the entered text.setName()updates the state.
3. Handling Form Submission with onSubmit
The onSubmit event runs when a form is submitted.
We commonly use it to validate the form or process the entered
information.
import { useState } from "react";
function StudentForm() {
const [studentName, setStudentName] = useState("");
const [message, setMessage] = useState("");
function handleSubmit(event) {
event.preventDefault();
setMessage("Welcome, " + studentName + "!");
}
return (
<form onSubmit={handleSubmit}>
<input
value={studentName}
onChange={(event) =>
setStudentName(event.target.value)
}
placeholder="Enter student name"
required
/>
<button type="submit">Register</button>
<p>{message}</p>
</form>
);
}
export default StudentForm;
preventDefault() prevents the normal browser
submission, and setMessage() displays a greeting.
4. What Is event.preventDefault()?
By default, submitting a normal HTML form can cause the browser to navigate or reload the page. In React, we often want to handle the submission ourselves.
function PreventDefault() {
function handleSubmit(event) {
event.preventDefault();
console.log("Default form submission prevented");
}
return (
<form onSubmit={handleSubmit}>
<button type="submit">
Submit Form
</button>
</form>
);
}
export default PreventDefault;
event.preventDefault() inside the
submit handler when you want to handle submission without
the browser's normal form action.
5. Handling Focus and Blur Events
The onFocus event occurs when an input receives
focus. The onBlur event occurs when the input
loses focus.
function FocusBlur() {
function handleFocus() {
console.log("Input focused");
}
function handleBlur() {
console.log("Input lost focus");
}
return (
<input
onFocus={handleFocus}
onBlur={handleBlur}
placeholder="Click here"
/>
);
}
export default FocusBlur;
onFocus.
Click somewhere else to trigger onBlur.
6. Handling a Select Dropdown
React can handle dropdown changes using
onChange. We can read the selected option and
display it to the user.
import { useState } from "react";
function CourseSelect() {
const [course, setCourse] = useState("");
return (
<div>
<select
value={course}
onChange={(event) =>
setCourse(event.target.value)
}>
<option value="">Select a course</option>
<option value="React">React</option>
<option value="Java">Java</option>
<option value="Python">Python</option>
</select>
<p>Selected course: {course}</p>
</div>
);
}
export default CourseSelect;
7. Common Form Event Mistakes
1. Forgetting preventDefault()
function handleSubmit(event) {
event.preventDefault();
console.log("Form submitted");
}
Prevent the browser's default form submission when handling the form in your React component.
2. Forgetting to connect onSubmit
// Correct
<form onSubmit={handleSubmit}>
<button type="submit">Submit</button>
</form>
Connect the submit handler to the form element.
3. Forgetting to update input state
function handleChange(event) {
setName(event.target.value);
}
For a controlled React input, update the state in
onChange.
8. Practice Questions
- What are form events in React?
- What is the purpose of
onChange? - How does
onSubmitwork? - Why do we use
event.preventDefault()? - What is the difference between
onFocusandonBlur? - Create a form that displays a greeting after submission.
- Create a dropdown that displays the selected course.
Summary
Form events help React components respond to user input and
form submission. Use onChange to track values,
onSubmit to handle submission, and
preventDefault() to stop the browser's default
form action when necessary. React state can store form values
and display the updated results.