日本語

Why a Button Inside a Form Submits in React

​​React​ESLint,Forms,HTML,TypeScript

Under HTML rules, a <button> inside a <form> with no type attribute is treated as a submit button. React does not add a type for you and outputs the element as it is, so clicking it submits the form. A form with no action set reloads the current page. If clicking the "show password" button on a login form reloads the page and clears your input, this is the cause. Adding type="button" to a button that should not submit stops the submission.

In a test environment, I built a "show" button with different type settings and checked what happens in Chromium. Along with the fix, I also cover how a type you think you wrote can still leave the button as a submit button, and how to find out which button submitted the form.

Note

The test environment was Ubuntu 24.04 and Node.js 22.23.4. I used react / react-dom 19.3.1, @types/react 19.3.0, TypeScript 6.0.3, esbuild 0.28.2, ESLint 9.39.5, eslint-plugin-react 7.37.5, and typescript-eslint 8.72.2. I bundled the .tsx files for development with esbuild and ran them in Chromium 153 driven by Playwright 1.63.0. I did not test StrictMode, build tools such as Vite, frameworks such as Next.js, or browsers other than Chromium.

Two forms stacked vertically. In the top form, labeled "no type", I type neko and click the show button. The reload count goes from 1 to 2, the URL changes to /Demo?password=neko, and the field is cleared. Next, in the bottom form labeled "type=\"button\"", I type neko and click the show button. The count stays at 2, and the field's text changes from masked characters to neko. (The labels in the image are in Japanese.)
The top shows the page reloading when the show button is clicked, which clears the input. The bottom shows only the password becoming visible.

A button with no type is a submit button under HTML

The HTML spec's description of the button element's type attribute defines three values: submit (submits the form), reset (resets inputs to their initial values), and button (does nothing). When the type attribute is missing, or has a value outside those three, the button is in the Auto state. An Auto-state button that has no command or commandfor attribute and is not a direct child of a select element acts as a submit button.

The MDN description of the button element's type attribute also says that a button associated with a form acts as submit when type is missing, empty, or invalid.

You can confirm this by inspecting the element in the browser. For the "show" button in the NoType component below, I read both getAttribute('type') and the type property.

tsx
import { useState } from 'react';

export function NoType() {
  const [show, setShow] = useState(false);
  return (
    <form>
      <input name="email" aria-label="メールアドレス" />
      <input name="password" type={show ? 'text' : 'password'} aria-label="パスワード" />
      <button onClick={() => setShow(!show)}>表示</button>
    </form>
  );
}

The type attribute was null (not written), but the type property returned "submit". The spec's description of the type property also says it returns "submit" for a submit button. Even without a type attribute, reading the type property tells you whether the button submits.

React does not add a type

The HTML React outputs has no type either. I used renderToStaticMarkup from react-dom/server to turn a button with and without type into strings.

tsx
import { renderToStaticMarkup } from 'react-dom/server';
console.log(renderToStaticMarkup(<button>表示</button>));
console.log(renderToStaticMarkup(<button type="button">表示</button>));
text
<button>表示</button>
<button type="button">表示</button>

The rendered NoType button's outerHTML was also <button>表示</button>. In the TypeScript types (@types/react), the type of button is "submit" | "reset" | "button" | undefined, so leaving it out does not cause a type error. This is not really a problem with React's API. It happens because React uses the plain HTML button element as it is.

How a form submission turns into a page reload

The spec's description of the button element says that when a clicked button belongs to a form and is a submit button, the form is submitted. The form submission algorithm uses the current page URL as the target when action is empty. The MDN description of the form element's method attribute says the default method is GET, and the form data is appended after ? in the URL.

These are the results when I entered an email address and password in NoType and clicked "show".

text
押す前: URL=/NoType / 読み込んだ回数:1 / パスワード欄 type=password 値=「nekoneko」
「表示」を押したあと: URL=/NoType?email=tama%40example.com&password=nekoneko / 読み込んだ回数:2 / パスワード欄 type=password 値=「」

The reload count went up, and the password field was cleared. The show state went back to its initial false, so the password stayed masked.

Warning

As the result shows, simply clicking a "show" button that is missing type put the password I had typed into the page URL.

Ways to write type and still get a submit button

The value is misspelled

If you misspell the value, as in type="buton", it becomes an invalid value under the spec and the button is a submit button, the same as when type is missing. In my test, the type property returned "submit", and clicking the button reloaded the page with the URL /Typo?password=nekoneko.

However, running a type check with tsc catches this before the build.

text
typo/typo.tsx(9,15): error TS2820: Type '"buton"' is not assignable to type '"button" | "reset" | "submit" | undefined'. Did you mean '"button"'?

esbuild converts code without checking types, so the build succeeded even in this state. If you do not run a separate type check, you will miss it.

A custom Button component does not accept type

To match styling, you may wrap button in your own component.

tsx
import type { ComponentProps } from 'react';

function Button(props: ComponentProps<'button'>) {
  return <button className="btn" {...props} />;
}

// Usage
<Button onClick={() => setShow(!show)}>表示</Button>

If the caller does not pass type, the inner button gets no type either. In my test, it acted as a submit button, the same as NoType, and clicking it reloaded the page with the URL /WrapperNoType?password=nekoneko.

Placed outside the form and linked with the form attribute

A button placed outside a <form> does not belong to the form, so clicking it submits nothing. In my test, the button outside the form did not change the URL when clicked, and the input stayed.

However, as the MDN description of the button element's form attribute explains, if you set form="search" to the form's id, the button belongs to that form even when it is outside. With no type, it is a submit button, so clicking it reloaded the page with the URL /Outside?q=たま.

Write type="button" on buttons that should not submit

The fix is to add type="button" to any button that should not submit.

tsx
<button type="button" onClick={() => setShow(!show)}>表示</button>

In my test, clicking it did not change the URL or the reload count, and the password field became type=text, showing "nekoneko". The type property returned "button".

If your custom Button component defaults to "button" when type is not passed, a forgotten type will not cause a submission.

tsx
import type { ComponentProps } from 'react';

// Button component that defaults to "button" when type is not passed (only "submit" makes a submit button)
function SafeButton({ type, ...props }: ComponentProps<'button'>) {
  return <button className="btn" type={type === 'submit' ? 'submit' : 'button'} {...props} />;
}

// Usage
<SafeButton onClick={() => setShow(!show)}>表示</SafeButton>
<SafeButton type="submit">ログイン</SafeButton>

With this form, type="reset" cannot be used. It is meant for forms that do not need a reset button. In my test, clicking "show" only made the password visible and did not call onSubmit. Only clicking the login button called onSubmit, which called preventDefault and displayed a message.

Also, I tested a version with no type that calls e.preventDefault() inside onClick. It did not submit in my test. However, the button's type property stayed "submit", and the ESLint rule introduced in a later section reported an error for it.

Finding which button submitted the form

If a form has several buttons and you cannot tell which one triggered the submission, check nativeEvent.submitter on the event passed to onSubmit. As the MDN description of SubmitEvent.submitter explains, it holds the button that triggered the submission.

tsx
import type { SubmitEvent } from 'react';

function handleSubmit(e: SubmitEvent<HTMLFormElement>) {
  e.preventDefault();
  const submitter = e.nativeEvent.submitter;
  console.log('送信したボタン:', submitter?.textContent, '/ e.submitter:', e.submitter?.textContent);
  setMessage('ログインします');
}

I placed a "show" button and a login button with no type. When I clicked "show", the console printed the following, and the "logging in" message was also displayed.

text
送信したボタン: 表示 / e.submitter: 表示

This shows that the "show" button, which was missing type, was also running the login logic. In the test environment (react-dom 19.3.1, @types/react 19.3.0), reading e.submitter without going through nativeEvent returned the same button.

Using the react/button-has-type ESLint rule to catch missing types

eslint-plugin-react has a rule called react/button-has-type that finds button elements with no type.

js
// eslint.config.js
import react from 'eslint-plugin-react';
import tseslint from 'typescript-eslint';

export default [
  {
    files: ['src/**/*.tsx'],
    languageOptions: { parser: tseslint.parser },
    plugins: { react },
    rules: { 'react/button-has-type': 'error' },
  },
];

When I applied this to a file that collects the examples in this article, the lines with a button that had no type were reported as errors. (I replaced the working folder path with /path/to/app.)

text
/path/to/app/src/cases.tsx
   11:7   error  Missing an explicit type attribute for button  react/button-has-type
   44:7   error  Missing an explicit type attribute for button  react/button-has-type
   45:7   error  Missing an explicit type attribute for button  react/button-has-type
   53:10  error  Missing an explicit type attribute for button  react/button-has-type
   92:7   error  Missing an explicit type attribute for button  react/button-has-type
   93:7   error  Missing an explicit type attribute for button  react/button-has-type
  105:7   error  Missing an explicit type attribute for button  react/button-has-type

Line 11 is NoType. Lines 44 and 45 are the "show" and "login" buttons in the example that finds the submitting button. Lines 92 and 93 are the buttons placed outside the form. Line 105 is the example that calls preventDefault in onClick. Line 53 is <button className="btn" {...props} /> inside the Button component. The lines that use <Button onClick={...}>表示</Button> did not produce errors. This rule checks the button element itself, so a custom component has to set type on the inner button. The type={type === 'submit' ? 'submit' : 'button'} in SafeButton did not produce an error.

​Posted by 86