Notification texts go here Contact Us Buy Now!

Using form with MUI onSubmit method not working

<Box component="form" onSubmit={submitHandler}>
    <TextField />
    <Button type="submit">Submit</Button>
</Box>

You are overriding your MUI component, so the Box is already assuming itself as a form component.

Consider reading the Overriding MUI components from the MUI documentation.

<form {...extraPropsFromBox}>
  <form onSubmit={submitHandler}>
    <TextField />
    <Button type="submit">Submit</Button>
  </form>
</form>

Nested form is not supported in HTML 5 and using it is asking for trouble, in this case it probably ignores the submit handler in the nested form, the fix is remove the outer one. If you don't have a need to style the form container, just use a normal form tag. Use Box + form if you want to apply some styles quickly:

<Box component="form" onSubmit={submitHandler} sx={{ maxWidth: 300, bgcolor: 'gray' }}>
    <TextField />
    <Button type="submit">Submit</Button>
</Box>

All MUI components have a component prop that allows you to override the root component. Below is what the Box looks like under-the-hook. You do not have to specify the component because they all have a default value:

function Box({ component = 'div', ...boxProps }) {
  return <component {...boxProps} />
}
<Button type="submit">Submit</Button>

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.