react/no-array-index-key Perf
它的作用
如果元素在其 key 中使用了数组索引,则发出警告。
为什么这不好?
使用数组索引是个坏主意,因为它不能唯一标识你的元素。 在数组被排序,或者有元素被添加到数组开头的情况下, 即使代表该索引的元素可能还是同一个,索引也会发生变化。 这会导致不必要的重新渲染。
示例
以下是此规则的错误代码示例:
jsx
things.map((thing, index) => <Hello key={index} />);以下是此规则的正确代码示例:
jsx
things.map((thing, index) => <Hello key={thing.id} />);如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["react"],
"rules": {
"react/no-array-index-key": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["react"],
rules: {
"react/no-array-index-key": "error",
},
});bash
oxlint --deny react/no-array-index-key --react-plugin版本
此规则于 v0.13.0 中添加。
