Skip to content
← Back to rules

react/button-has-type 限制

它的作用

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

为什么这是个问题?

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

示例

此规则的 错误 代码示例:

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

此规则的 正确 代码示例:

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

配置

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

button

type: boolean

默认值: true

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

reset

type: boolean

默认值: true

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

submit

type: boolean

默认值: true

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

如何使用

要通过配置文件或 CLI 启用 此规则,可以使用:

json
{
  "plugins": ["react"],
  "rules": {
    "react/button-has-type": "error"
  }
}
bash
oxlint --deny react/button-has-type --react-plugin

参考资料