useJsxKeyInIterable
Summary
Section titled “Summary”- Rule available since:
v1.6.0
- Diagnostic Category:
lint/correctness/useJsxKeyInIterable
- This rule doesn’t have a fix.
- The default severity of this rule is error.
- This rule belongs to the following domains:
- Sources:
- Same as
react/jsx-key
- Same as
Description
Section titled “Description”Disallow missing key props in iterators/collection literals.
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.
Examples
Section titled “Examples”Invalid
Section titled “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.
data.map((x) => <Hello>{x}</Hello>);
code-block.jsx:1:17 lint/correctness/useJsxKeyInIterable ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Missing key property for this element in iterable.
> 1 │ data.map((x) => <Hello>{x}</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.
[<Hello key="first" />, <Hello key="second" />, <Hello key="third" />];data.map((x) => <Hello key={x.id}>{x}</Hello>);
Options
Section titled “Options”checkShorthandFragments
Section titled “checkShorthandFragments”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.
{ "options": { "checkShorthandFragments": true }}
data.map((x) => <>{x}</>);
How to configure
Section titled “How to configure”{ "linter": { "rules": { "correctness": { "useJsxKeyInIterable": "error" } } }}