Maaz Zindani

Loading the index…

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

Submitting a React form from code skips the onSubmit handler

ErrorformRef.current.submit() reloads the page or posts the form, but onSubmit is never called

Short answer

The DOM method form.submit() only submits the form and does nothing else, so no submit event fires and React's onSubmit never runs. Call form.requestSubmit() instead. It acts as if a submit button were clicked, so validation and your handler both run. It throws if the submitter you pass is not a submit button of that form.

  • Applies to React 19, current Chrome, Firefox and Safari
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Search for the call that submits the form from code. If it says submit(), that is the cause.

    grep -rn "\.submit()" src/
  2. 2Match what you see, then jump to its fix

    1. 1The page navigates or posts, but your onSubmit log line is missing and required fields are not checked.form.submit() was used instead of requestSubmit()Fix
    2. 2TypeError: submit is not a functionA field named submit hides the form's submit methodFix
    3. 3TypeError or NotFoundError thrown from requestSubmit(button)The submitter passed to requestSubmit() is not a submit button of this formFix

What does the error look like?

  • formRef.current.submit() reloads the page or posts the form, but onSubmit is never called
  • console.log inside the onSubmit handler never prints
  • TypeError: submit is not a function

Cause 1 of 3

form.submit() was used instead of requestSubmit()

1Check

MDN says no submit event is raised by submit(), so the onsubmit handler is not run, and constraint validation is not triggered. requestSubmit() behaves like clicking a submit button.

// Before: bypasses onSubmit and validation
formRef.current.submit();

2Fix

Call requestSubmit() on the form element.

function Checkout() {
  const formRef = useRef(null);

  function handleSubmit(e) {
    e.preventDefault();
    console.log("submitted");
  }

  return (
    <>
      <form ref={formRef} onSubmit={handleSubmit}>
        <input name="email" type="email" required />
      </form>
      <button type="button" onClick={() => formRef.current.requestSubmit()}>
        Pay
      </button>
    </>
  );
}

3Verify

Click the outside button with an empty required field; the browser should show its validation message, and with a valid value onSubmit should log.

npm run dev

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 3

A field named submit hides the form's submit method

1Check

MDN notes that a form control with a name or id of submit masks the method, so the call throws.

grep -rn "name=\"submit\"\|id=\"submit\"" src/

2Fix

Rename the control (for example to save or send).

<button type="submit" name="save">Save</button>

3Verify

Reload and call the form method again; the TypeError should be gone.

npm run dev

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 3

The submitter passed to requestSubmit() is not a submit button of this form

1Check

MDN lists two exceptions: TypeError when the submitter is not a submit button, and NotFoundError when it is not a member of the form. The button must be inside the form or point to it with the form attribute.

// type="button" is not a submit button
<button type="button" id="save">Save</button>

2Fix

Pass a real submit button that belongs to the form, or call requestSubmit() with no argument so the form itself is the submitter.

// Option 1: no argument
formRef.current.requestSubmit();

// Option 2: a submit button linked with the form attribute
<form id="checkout-form" onSubmit={handleSubmit}>...</form>
<button type="submit" form="checkout-form" ref={saveRef}>Save</button>
// saveRef.current can then be passed to requestSubmit(saveRef.current)

3Verify

Call it again; no exception should appear in the console and onSubmit should run.

npm run dev

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Use requestSubmit() for every programmatic submit; reserve submit() for when you really want to skip validation and the event.
  • Put submit logic in one onSubmit handler so a button click, the Enter key and code all go through 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

Next step

Want this handled for good?

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