Maaz Zindani

Loading the index…

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

React Hook Form: the submit button does nothing and onSubmit never runs

ErrorClicking submit does nothing and onSubmit is never logged

Short answer

handleSubmit runs your onSubmit only when validation passes. When nothing happens, validation usually failed silently: pass a second callback or print formState.errors to see which field. Other causes are wrapping handleSubmit in an arrow function, which reloads the page, and a submit button that sits outside the form element.

  • Applies to react-hook-form 7, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Give handleSubmit a second argument that logs the validation errors. If it prints, validation is the reason onSubmit stays silent.

    <form onSubmit={handleSubmit(onSubmit, (errors) => console.log('invalid', errors))}>
  2. 2Match what you see, then jump to its fix

    1. 1The second (error) callback runs, or formState.errors is not emptyValidation failed, so the valid callback is skippedFix
    2. 2The page reloads on submit and onSubmit is not calledhandleSubmit is wrapped in an arrow functionFix
    3. 3Clicking the button does nothing, and the button is placed outside the form tag in the JSXThe submit button is outside the form elementFix
    4. 4Submitting an inner form in a modal also fires the outer form's handlerA nested form submits the outer form as wellFix

What does the error look like?

  • Clicking submit does nothing and onSubmit is never logged
  • The page reloads when the form is submitted

Cause 1 of 4

Validation failed, so the valid callback is skipped

1Check

handleSubmit validates the inputs first. Only if they pass does it call onSubmit; if they fail it calls the optional second callback with the errors. A required field the user left empty is enough.

const { register, handleSubmit, formState: { errors } } = useForm();
console.log(errors);

2Fix

Fix the field the errors name, or show errors next to the inputs so users see them too.

<input {...register('email', { required: 'Email is required' })} />
{errors.email && <p role="alert">{errors.email.message}</p>}

3Verify

Fill the field and submit. onSubmit now logs the form data.

# click submit with the browser console open

Confirmed by react-hook-form.com

Did this fix it?

Cause 2 of 4

handleSubmit is wrapped in an arrow function

1Check

handleSubmit(onSubmit) returns a function that takes the event. Wrapping the call in an arrow function runs it without the event, so the browser performs its default submit.

grep -rn "onSubmit={" src/

2Fix

Pass the result of handleSubmit directly.

// wrong
<form onSubmit={() => handleSubmit(onSubmit)}>
// right
<form onSubmit={handleSubmit(onSubmit)}>

3Verify

Submit the form. The page does not reload and onSubmit runs once validation passes.

# click submit and watch the network tab for a full page load

Confirmed by react-hook-form.com

Did this fix it?

Cause 3 of 4

The submit button is outside the form element

1Check

A button that is not inside the form does not submit it. Look at where the button sits in the JSX, for example in a modal footer.

grep -rn "type=\"submit\"" src/

2Fix

Link the button to the form with the native form attribute, or call handleSubmit(onSubmit) in the button's onClick.

<form id="profile-form" onSubmit={handleSubmit(onSubmit)}>...</form>
<button type="submit" form="profile-form">Save</button>

3Verify

Click the button. onSubmit runs, or the error callback prints the errors.

# click the button with the console open

Confirmed by react-hook-form.com

Did this fix it?

Cause 4 of 4

A nested form submits the outer form as well

1Check

When one form sits inside another form's tree, as with a modal, the submit event bubbles up to the outer form.

grep -rn "<form" src/

2Fix

Stop the event from bubbling in the inner form's submit handler.

<form onSubmit={(e) => { e.stopPropagation(); handleSubmit(onInnerSubmit)(e); }}>

3Verify

Submit the inner form. Only its own handler runs.

# click the inner submit with the console open

Confirmed by react-hook-form.com

Did this fix it?

How do I stop it coming back?

  • Always pass the error callback or render formState.errors while building a form.
  • Keep the submit button inside the form, or give it the form attribute.
  • Await async work in onSubmit so formState.isSubmitting stays true until it finishes.

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.