The form still reloads the page in React with TypeScript
The page refreshes and the form fields clear when you press the submit buttonShort answer
A form reloads when its submit event is not cancelled. Put the handler on the form's onSubmit, not on a click, call e.preventDefault() in it, and type the event as React.SyntheticEvent or FormEvent. If you pass a function to action, React handles submission and you do not need preventDefault.
Fix it fast
1Find out which cause you have
Add a log as the first line of the handler. If it never prints, the handler is not on the form's submit event. If it prints and the page still reloads, the event type or an action prop is the problem.
function handleSubmit(e: React.SyntheticEvent<HTMLFormElement>) { console.log("submit fired", e.type); e.preventDefault(); }2Match what you see, then jump to its fix
- 1
Nothing logs on submit, or the page reloads even though a button has onClickThe handler is not on the form's onSubmitFix - 2
The log prints and the page reloads afterwardspreventDefault is missing, or runs too lateFix - 3
tsc or the editor reports an error on the handler argumentThe event has no type, so TypeScript rejects or hides the callFix - 4
The form has action={someFunction}The form uses action with a function, not onSubmitFix
- 1
What does the error look like?
The page refreshes and the form fields clear when you press the submit buttonThe URL gains a ?name=value query string after submitThe submit handler logs once and the form values are gone
Cause 1 of 4
The handler is not on the form's onSubmit
1Check
Find where the function is attached. A button inside a form with no type attribute is a submit button, so it still submits whatever its onClick does.
grep -rn "onClick\|onSubmit" src/components/ContactForm.tsx2Fix
Attach the handler to the form, and give buttons that are not meant to submit type="button".
<form onSubmit={handleSubmit}>
<input name="query" />
<button type="submit">Search</button>
<button type="button" onClick={clearFilters}>Clear</button>
</form>3Verify
Press the submit button: the handler logs and the page stays.
// watch the browser consoleConfirmed by developer.mozilla.org
Cause 2 of 4
preventDefault is missing, or runs too late
1Check
By default the browser sends the form data to the current URL and refreshes the page. Only e.preventDefault() overrides that, and it must run while the event is being handled.
grep -n "preventDefault" src/components/ContactForm.tsx2Fix
Call e.preventDefault() as the first line of the handler, before any other code that could throw.
function handleSubmit(e: React.SyntheticEvent<HTMLFormElement>) {
e.preventDefault();
// read the values after this line
}3Verify
Submit the form. The URL does not change and the fields keep their values.
// watch the address barConfirmed by react.dev
Cause 3 of 4
The event has no type, so TypeScript rejects or hides the call
1Check
React infers the type when the function is written inline. A handler you extract needs the type written by hand.
npx tsc --noEmit2Fix
Type the parameter. React.SyntheticEvent is the base type for every event, so it is always valid.
function handleSubmit(e: React.SyntheticEvent<HTMLFormElement>) {
e.preventDefault();
}3Verify
tsc finishes with no error on that file.
npx tsc --noEmitConfirmed by react.dev
Cause 4 of 4
The form uses action with a function, not onSubmit
1Check
When action is a function, React runs it on submit and passes it a FormData object. You do not need onSubmit or preventDefault in that case. If you also have onSubmit, React runs the action after it unless onSubmit calls e.preventDefault().
grep -n "action=" src/components/ContactForm.tsx2Fix
Keep one style. Either use action with a function that takes FormData, or use onSubmit with preventDefault, not both.
async function search(formData: FormData) {
const query = formData.get("query");
}
<form action={search}>...</form>3Verify
The function runs once per submit and the page does not reload.
// watch the network tab and the consoleConfirmed by react.dev
How do I stop it coming back?
- Give every button inside a form an explicit type.
- Type extracted handlers with React.SyntheticEvent so tsc catches a missing argument.
- Choose either onSubmit or action for a form and stay with it.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
TypeScript refuses JSX because the jsx option is not set
TS17004: Cannot use JSX unless the '--jsx' flag is provided.
TypeScript compiles JSX only when you name the file with a .tsx extension and enable the jsx compiler option.
2 causes, each with a fixWeb apps
TypeScript rejects a component that returns an array or other non-Element
Its return type 'Element[]' is not a valid JSX element.
TypeScript before 5.1 required every component to return JSX.Element or null, so arrays, strings and Promises were rejected even though React renders them.
2 causes, each with a fixWeb apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fixWeb apps
ESLint reports no-unused-vars for React components and imports that are used
no-unused-vars reported for a component that is rendered as a JSX tag
ESLint's core no-unused-vars does not see variables used only as JSX tags, so a component imported and rendered as <Hello /> looks unused.
3 causes, each with a fixWeb apps
TypeScript rejects MUI Data Grid columns that set a type
A TypeScript error inside a column's renderCell callback
A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.
2 causes, each with a fixWeb apps
The renderInput prop stopped working on the MUI X DatePicker
The custom TextField (variant, size, helperText) is ignored
renderInput was removed from the MUI X date pickers in version 6.
3 causes, each with a fix