onSubmit fires when a form is submitted, either by clicking a submit-type button inside it or by pressing Enter within a text input inside the form — by default, a browser's native form submission triggers a full page reload/navigation, which is almost never the desired behavior in a React single-page application, so calling event.preventDefault() as the very first line inside the handler is the standard, near-universal pattern to stop that default behavior and handle the submitted data with JavaScript instead.
1Understanding onSubmit
onSubmit fires when a form is submitted, either by clicking a submit-type button inside it or by pressing Enter within a text input inside the form — by default, a browser's native form submission triggers a full page reload/navigation, which is almost never the desired behavior in a React single-page application, so calling event.preventDefault() as the very first line inside the handler is the standard, near-universal pattern to stop that default behavior and handle the submitted data with JavaScript instead.
Call event.preventDefault() as the first line of virtually every onSubmit handler in a React app — forgetting it causes the browser's default full-page reload/navigation to happen, which typically breaks the single-page application experience entirely.
function LoginForm() {
const handleSubmit = (event) => {
event.preventDefault();
console.log('Form submitted, no page reload occurred');
};
return (
<form onSubmit={handleSubmit}>
<input type="text" name="username" />
<button type="submit">Log In</button>
</form>
);
}2Practical Example
Here is a real-world application of onSubmit showing how it is used in production React code.
function ContactForm() {
const handleSubmit = (event) => {
event.preventDefault();
const formData = new FormData(event.target);
console.log('Email:', formData.get('email'));
};
return (
<form onSubmit={handleSubmit}>
<input type="email" name="email" defaultValue="user@example.com" />
<button type="submit">Send</button>
</form>
);
}3Best Practices
Follow these guidelines when working with onSubmit:
1. Call event.preventDefault() at the start of an onSubmit handler to stop the browser's default full-page reload/navigation
2. Attach onSubmit to the form element itself, not to the submit button's onClick, so the handler correctly fires for both button clicks and pressing Enter in an input
3. Perform form validation, then any actual data submission logic like an API call, inside the onSubmit handler after preventing the default behavior
Tip: Call event.preventDefault() as the first line of virtually every onSubmit handler in a React app — forgetting it causes the browser's default full-page reload/navigation to happen, which typically breaks the single-page application experience entirely.
function LoginForm() {
const handleSubmit = (event) => {
event.preventDefault();
console.log('Form submitted, no page reload occurred');
};
return (
<form onSubmit={handleSubmit}>
<input type="text" name="username" />
<button type="submit">Log In</button>
</form>
);
}