Skip to content
← Back to rules

react/button-has-type Restriction

作用

强制所有 button HTML 元素显式指定 type 属性。

为什么这不好?

button HTML 元素的 type 属性默认值是 "submit",这通常不是期望的行为,并且可能导致 意外的页面重新加载。

示例

以下是此规则的错误代码示例:

jsx
<button />
<button type="foo" />

以下是此规则的正确代码示例:

jsx
<button type="button" />
<button type="submit" />

配置

此规则接受一个包含以下属性的配置对象:

button

type: boolean

default: true

如果为 true,则允许 type="button"

reset

type: boolean

default: true

如果为 true,则允许 type="reset"

submit

type: boolean

default: true

如果为 true,则允许 type="submit"

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["react"],
  "rules": {
    "react/button-has-type": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["react"],
  rules: {
    "react/button-has-type": "error",
  },
});
bash
oxlint --deny react/button-has-type --react-plugin

版本

此规则在 v0.1.1 中添加。

参考