Submitting a React form from code skips the onSubmit handler
formRef.current.submit() reloads the page or posts the form, but onSubmit is never calledShort 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.
Fix it fast
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/2Match what you see, then jump to its fix
- 1
The 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
TypeError: submit is not a functionA field named submit hides the form's submit methodFix - 3
TypeError or NotFoundError thrown from requestSubmit(button)The submitter passed to requestSubmit() is not a submit button of this formFix
- 1
What does the error look like?
formRef.current.submit() reloads the page or posts the form, but onSubmit is never calledconsole.log inside the onSubmit handler never printsTypeError: 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 devConfirmed by developer.mozilla.org
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 devConfirmed by developer.mozilla.org
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 devConfirmed by developer.mozilla.org
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:
- 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
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix