Skip to content
← Back to rules

react/jsx-fragments Style

🛠️ An auto-fix is available for this rule.

功能

强制 React Fragments 使用简写形式或标准形式。

为什么这不好?

让使用片段的代码在某种程度上更加一致。

配置

此规则接受以下字符串值之一:

"syntax"

这是默认模式。它会强制 React fragments 使用简写语法,但有一个例外。 简写语法不支持 key 或属性,因此对于使用这些内容的标准形式片段,规则不会发出警告。

此规则的错误示例代码:

jsx
<React.Fragment>
  <Foo />
</React.Fragment>

此规则的正确示例代码:

jsx
<>
  <Foo />
</>
jsx
<React.Fragment key="key">
  <Foo />
</React.Fragment>

"element"

此模式强制 React fragments 使用标准形式。

此规则的错误示例代码:

jsx
<>
  <Foo />
</>

此规则的正确示例代码:

jsx
<React.Fragment>
  <Foo />
</React.Fragment>
jsx
<React.Fragment key="key">
  <Foo />
</React.Fragment>

使用方法

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

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

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

版本

此规则在 v1.12.0 中添加。

参考资料