Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

The form still reloads the page in React with TypeScript

ErrorThe page refreshes and the form fields clear when you press the submit button

Short 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.

  • Applies to React 19, TypeScript 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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();
    }
  2. 2Match what you see, then jump to its fix

    1. 1Nothing logs on submit, or the page reloads even though a button has onClickThe handler is not on the form's onSubmitFix
    2. 2The log prints and the page reloads afterwardspreventDefault is missing, or runs too lateFix
    3. 3tsc or the editor reports an error on the handler argumentThe event has no type, so TypeScript rejects or hides the callFix
    4. 4The form has action={someFunction}The form uses action with a function, not onSubmitFix

What does the error look like?

  • The page refreshes and the form fields clear when you press the submit button
  • The URL gains a ?name=value query string after submit
  • The 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.tsx

2Fix

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 console

Confirmed by developer.mozilla.org

Did this fix it?

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.tsx

2Fix

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 bar

Confirmed by react.dev

Did this fix it?

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 --noEmit

2Fix

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 --noEmit

Confirmed by react.dev

Did this fix it?

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.tsx

2Fix

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 console

Confirmed by react.dev

Did this fix it?

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:

Related fixes

Web 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.