react-perf/jsx-no-new-array-as-prop Perf
作用
防止将仅在当前函数中局部创建的数组用作 JSX props 的值。
为什么这不好?
将局部定义的数组用作 props 的值可能会导致非预期的重新渲染和性能问题。每次父组件渲染时,都会创建一个新的数组实例,从而导致子组件不必要的重新渲染。这也会使代码更难维护,因为组件的 props 传递不再保持一致。
示例
此规则的错误代码示例:
jsx
<Item list={[]} />
<Item list={new Array()} />
<Item list={Array()} />
<Item list={this.props.list || []} />
<Item list={this.props.list ? this.props.list : []} />此规则的正确代码示例:
jsx
<Item list={this.props.list} />如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["react-perf"],
"rules": {
"react-perf/jsx-no-new-array-as-prop": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["react-perf"],
rules: {
"react-perf/jsx-no-new-array-as-prop": "error",
},
});bash
oxlint --deny react-perf/jsx-no-new-array-as-prop --react-perf-plugin版本
此规则于 v0.2.3 中添加。
