Fixing Page Reload on Form Submit in React
If a search or login form built with React reloads the page and clears the typed text or state when you click the button or press Enter, the browser is submitting the <form> as a regular form. Calling e.preventDefault() on the first line of the function passed to onSubmit stops this. However, even with preventDefault written, the reload can still happen depending on where await appears and whether an error is thrown in an earlier line. If you forget to write type on a button that should not submit, an unintended submission can also happen.
In a test environment, I created six patterns that cause a reload or an unintended submission and operated them in Chromium to confirm the behavior. For each cause, I list the symptom and the fix.
The test environment was Ubuntu 24.04 and Node.js 22.23.4. The project was created with the react-ts template of npm create vite@latest (react / react-dom 19.3.1, Vite 8.3.8, TypeScript 7.0.2). The components were wrapped in StrictMode and opened with the dev server (vite). I ran the operations in Chromium 153 using Playwright 1.63.0. I did not test frameworks such as Next.js, form libraries such as React Hook Form, browsers other than Chromium, or the keyboards on smartphones.
- 1. How to tell whether the page reloaded
- 2. preventDefault is not called in onSubmit
- 3. Handling only with onClick on the button
- 4. Not writing type on a button that should not submit
- 5. All buttons are type="button", but Enter still reloads the page
- 6. Calling preventDefault after await
- 7. An error is thrown on a line before preventDefault
- 8. React 19 also allows passing a function to action
How to tell whether the page reloaded
When the page reloads, the field's name and value are appended to the end of the page URL. In my test, when I entered "tama" in the field with name="q" and submitted, /NoPrevent changed to /NoPrevent?q=%E3%81%9F%E3%81%BE. The input field was emptied, and the message that had been shown from state also disappeared.
As described in the official reference on handling form submission with an event handler, by default the browser sends the form data to the current URL and reloads the page. According to the MDN description of the method attribute of the form element, if method is not specified, the data is sent with GET and appended after the ? in the URL. This is why ?q=%E3%81%9F%E3%81%BE was added to the end of the URL.
In the GIF below, the top form does not call preventDefault, and the bottom form does. At the top, I show the number of times the page has been loaded in this tab and the current URL.

preventDefault is not called in onSubmit
This is the simplest case. Even if you pass a function to onSubmit, if preventDefault is not called inside it, the browser still performs its own submission after the function runs.
// Incorrect: preventDefault is not called in onSubmit
import { useState } from 'react';
export default function NoPrevent() {
const [message, setMessage] = useState('');
function handleSubmit() {
setMessage('検索しました');
}
return (
<form onSubmit={handleSubmit}>
<input name="q" aria-label="キーワード" />
<button>検索</button>
<p>{message}</p>
</form>
);
}
When I entered "tama" and pressed the search button, the URL changed to include ?q=%E3%81%9F%E3%81%BE, and both the field and the message were cleared. Pressing Enter in the input field instead of clicking the button produced the same result.
In the function passed to onSubmit, receive the event and call e.preventDefault() first. The official events guide example for preventing default browser behavior also uses this form.
// Fixed: call preventDefault first in onSubmit
import { useState, type FormEvent } from 'react';
export default function WithPrevent() {
const [message, setMessage] = useState('');
function handleSubmit(e: FormEvent<HTMLFormElement>) {
e.preventDefault();
const q = new FormData(e.currentTarget).get('q');
setMessage(`「${q}」で検索しました`);
}
return (
<form onSubmit={handleSubmit}>
<input name="q" aria-label="キーワード" />
<button>検索</button>
<p>{message}</p>
</form>
);
}
Whether I clicked the button or pressed Enter, the URL stayed at /WithPrevent, "tama" remained in the field, and the message "Searched for tama" was displayed.
Handling only with onClick on the button
This is the case where onSubmit is not passed to the <form>, and the processing is written in the button's onClick.
// Incorrect: onClick only on a button inside the form
<form>
<input name="q" aria-label="キーワード" />
<button onClick={() => setMessage('検索しました')}>検索</button>
<p>{message}</p>
</form>
When I pressed the search button, the page reloaded and the URL became /OnClickOnly?q=%E3%81%9F%E3%81%BE. This is because a <button> without a type is treated as the submit button of the form. The MDN description of the type attribute of the button element states that a button associated with a form whose type is not specified becomes submit.
If the processing is meant to submit the form, write it in <form>'s onSubmit and call preventDefault, as in the previous section, rather than in onClick. This way, the same function handles both a button click and an Enter key press.
Not writing type on a button that should not submit
Even if preventDefault is called in onSubmit, placing a non-submitting button such as "Clear" in the same form causes another problem.
// Incorrect: the "Clear" button does not have a type
import { useState, type FormEvent } from 'react';
export default function ClearButton() {
const [q, setQ] = useState('');
const [message, setMessage] = useState('');
function handleSubmit(e: FormEvent<HTMLFormElement>) {
e.preventDefault();
setMessage(`「${q}」で検索しました`);
}
return (
<form onSubmit={handleSubmit}>
<input value={q} onChange={(e) => setQ(e.target.value)} aria-label="キーワード" />
<button onClick={() => setQ('')}>クリア</button>
<button>検索</button>
<p>{message}</p>
</form>
);
}
Because preventDefault is present, the page is not reloaded. However, when I clicked the Clear button, the field was emptied and handleSubmit also ran, showing the message "Searched for" with an empty query. This is because the Clear button is also a submit button.
Furthermore, when I entered "tama" in the field and pressed Enter, the Clear behavior ran instead of the search. The field was emptied and the same empty-query message was displayed. The HTML specification section on implicit submission, which covers submission by the Enter key and similar actions, defines the first submit button in the form as the default button, and when submitting with the Enter key, a click event is sent to that button. In this example, the first submit button was the Clear button.
Give non-submitting buttons type="button". It is also helpful to write type="submit" on the submitting button so that readers can see the intent.
<button type="button" onClick={() => setQ('')}>クリア</button>
<button type="submit">検索</button>
After this fix, the Clear button only emptied the field and did not show a message. Pressing Enter displayed the message "Searched for tama".
All buttons are type="button", but Enter still reloads the page
This is the case where type="button" from the previous section is also applied to the search button, and the processing is written in onClick.
// Incorrect: all buttons are type="button", but the form has no onSubmit
<form>
<input name="q" aria-label="キーワード" />
<button type="button" onClick={() => setMessage('検索しました')}>検索</button>
<p>{message}</p>
</form>
When I pressed the search button, the page was not reloaded and the message "Searched" was displayed. However, when I pressed Enter in the input field, the URL changed to /EnterOnly?q=%E3%81%9F%E3%81%BE and the page reloaded.
The same section of the HTML specification mentioned above says that even a form without a submit button is submitted by the Enter key and similar actions if it has exactly one input field that accepts text or similar input. If there are two or more such fields, it is not submitted. In my test, a form with the same structure but two text input fields did not reload when Enter was pressed, and the onClick processing did not run either.
If you want a form to be submitted by the Enter key, the straightforward approach is to restore the button to a submit button and move the processing to onSubmit, calling preventDefault. This gives the WithPrevent form from the first section.
Calling preventDefault after await
This is the case where an async function is passed to onSubmit, and preventDefault is called after waiting for something such as a server request with await.
// Incorrect: preventDefault is called after await
async function handleSubmit(e: FormEvent<HTMLFormElement>) {
const q = String(new FormData(e.currentTarget).get('q'));
const result = await search(q); // A function that returns the result after waiting 300 ms
e.preventDefault();
setMessage(result);
}
In my test, when I pressed the search button, the page reloaded, the URL became /AsyncLate?q=%E3%81%9F%E3%81%BE, and no message was shown. As described in the MDN explanation of async functions, an async function suspends at the first await and returns control to its caller. In this example, preventDefault has not been called at that point, so the event handling simply ends, and the browser submits the form by its default behavior and reloads the page.
Call preventDefault before any await, on the first line of the function.
// Fixed: call preventDefault before await
async function handleSubmit(e: FormEvent<HTMLFormElement>) {
e.preventDefault();
const q = String(new FormData(e.currentTarget).get('q'));
const result = await search(q);
setMessage(result);
}
After this fix, the URL did not change and the message "Searched for tama" was displayed.
An error is thrown on a line before preventDefault
Even if preventDefault is written, if an error occurs on a line before it, the code never reaches preventDefault.
// Incorrect: an error is thrown on a line before preventDefault
type Settings = { search?: { limit: number } };
const settings: Settings = {};
function handleSubmit(e: FormEvent<HTMLFormElement>) {
const limit = settings.search!.limit; // Throws here because search is undefined
e.preventDefault();
setMessage(`${limit}件まで検索しました`);
}
When I pressed the search button, the error Cannot read properties of undefined (reading 'limit') was thrown, and the page reloaded immediately, changing the URL to /ThrowBefore?q=%E3%81%9F%E3%81%BE.
I also tested a version where preventDefault was moved to the first line, followed by a line that throws the same error. The same error appeared, but the page was not reloaded. The URL stayed at /ThrowAfter, and "tama" remained in the field. The error itself still needs to be fixed, but in this form the typed text was not lost.
In the function passed to onSubmit, call e.preventDefault() before anything else. If you call it before any await or any code that might throw, the reload is stopped.
React 19 also allows passing a function to action
As described in the React 19 release announcement, starting with React 19, you can pass a function to the action of <form>. According to the official reference on handling form submission with an action prop, with this approach you do not need onSubmit or e.preventDefault(). React receives the submission and passes the values of the fields with name to the function as FormData.
// React 19: pass a function to the form's action
import { useState } from 'react';
export default function ActionFn() {
const [message, setMessage] = useState('');
function search(formData: FormData) {
setMessage(`「${formData.get('q')}」で検索しました`);
}
return (
<form action={search}>
<input name="q" aria-label="キーワード" />
<button>検索</button>
<p>{message}</p>
</form>
);
}
In the test environment (react-dom 19.3.1), whether I clicked the button or pressed Enter, the URL did not change and the message "Searched for tama" was displayed. However, the input field was emptied. As described in the official reference on preserving form values after an action, when the function succeeds, React resets input fields that are not managed by value (uncontrolled inputs) to empty. If you want to keep the input, use the method described in that reference.





