react/no-children-prop 正确性
作用
检查是否通过 prop 传递 children。
为什么这不好?
children 应始终是实际的子元素,而不是通过 prop 传入。 使用 JSX 时,children 应嵌套在开始标签和结束标签之间。 不使用 JSX 时,children 应作为额外参数传递给 React.createElement。
示例
以下是此规则的错误代码示例:
jsx
<div children='Children' />
<MyComponent children={<AnotherComponent />} />
<MyComponent children={['Child 1', 'Child 2']} />
React.createElement("div", { children: 'Children' })以下是此规则的正确代码示例:
jsx
<div>Children</div>
<MyComponent>Children</MyComponent>
<MyComponent>
<span>Child 1</span>
<span>Child 2</span>
</MyComponent>
React.createElement("div", {}, 'Children')
React.createElement("div", 'Child 1', 'Child 2')如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["react"],
"rules": {
"react/no-children-prop": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["react"],
rules: {
"react/no-children-prop": "error",
},
});bash
oxlint --deny react/no-children-prop --react-plugin版本
此规则添加于 v0.0.14。
