Skip to content

useJsxKeyInIterable

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"correctness": {
"useJsxKeyInIterable": "error"
}
}
}
}

¥Description

禁止在迭代器/集合文字中缺少关键属性。

¥Disallow missing key props in iterators/collection literals.

如果元素可能需要 key prop(即数组文字或箭头函数表达式中存在的元素),则发出警告。检查格式、检查导入是否组织好以及 lint

¥Warn if an element that likely requires a key prop—namely, one present in an array literal or an arrow function expression. Check out React documentation for explanation on the why does React need keys.

此规则适用于 React 和 Qwik 应用,以防止迭代器内部的 JSX 元素缺少关键 prop。

¥This rule is intended for use in both React and Qwik applications to prevent missing key props in JSX elements inside iterators.

¥Examples

¥Invalid

[<Hello />];
code-block.jsx:1:2 lint/correctness/useJsxKeyInIterable ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Missing key property for this element in iterable.

> 1 │ [<Hello />];
^^^^^^^^^
2 │

The order of the items may change, and having a key can help React identify which item was moved.

Check the React documentation.

{items.map(item => <li>{item}</li>)}
code-block.jsx:1:20 lint/correctness/useJsxKeyInIterable ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Missing key property for this element in iterable.

> 1 │ {items.map(item => <li>{item}</li>)}
^^^^
2 │

The order of the items may change, and having a key can help React identify which item was moved.

Check the React documentation.

¥Valid

[<Hello key="first" />, <Hello key="second" />, <Hello key="third" />];
{items.map(item => <li key={item.id}>{item}</li>)}

¥Options

React 片段不仅可以使用 <React.Fragment> 创建,还可以使用简写片段 (<></>)。要同时检查这些文件是否需要键,请将 true 传递给此选项。

¥React fragments can not only be created with <React.Fragment>, but also with shorthand fragments (<></>). To also check if those require a key, pass true to this option.

biome.json
{
"linter": {
"rules": {
"correctness": {
"useJsxKeyInIterable": {
"options": {
"checkShorthandFragments": true
}
}
}
}
}
}
data.map((x) => <>{x}</>);
code-block.jsx:1:17 lint/correctness/useJsxKeyInIterable ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Missing key property for this element in iterable.

> 1 │ data.map((x) => <>{x}</>);
^^^^^^^^
2 │

The order of the items may change, and having a key can help React identify which item was moved.

Check the React documentation.

¥Related links