Back to Biomejs

useJsxKeyInIterable

src/content/docs/linter/rules/use-jsx-key-in-iterable.mdx

latest7.5 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JSX and TSX" icon="seti:javascript"> ## Summary - Rule available since: `v1.6.0` - Diagnostic Category: [`lint/correctness/useJsxKeyInIterable`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule doesn't have a fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - This rule belongs to the following domains: - [`react`](/linter/domains#react) - [`qwik`](/linter/domains#qwik) - Sources: - Same as [`react/jsx-key`](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-key.md) - Same as [`qwik/jsx-key`](https://qwik.dev/docs/advanced/eslint/#jsx-key)

How to configure

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

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.

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

Examples

Invalid

jsx
[<Hello />];
<pre class="language-text"><code class="language-text">code-block.jsx:1:2 <a href="https://biomejs.dev/linter/rules/use-jsx-key-in-iterable">lint/correctness/useJsxKeyInIterable</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Missing </span><span style="color: Tomato;"><strong>key</strong></span><span style="color: Tomato;"> property for this element in iterable.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>[&lt;Hello /&gt;]; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The order of the items may change, and having a key can help React identify which item was moved.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Check the </span><span style="color: lightgreen;"><a href="https://react.dev/learn/rendering-lists#why-does-react-need-keys">React documentation</a></span><span style="color: lightgreen;">. </span> </code></pre>
jsx
{items.map(item => <li>{item}</li>)}
<pre class="language-text"><code class="language-text">code-block.jsx:1:20 <a href="https://biomejs.dev/linter/rules/use-jsx-key-in-iterable">lint/correctness/useJsxKeyInIterable</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Missing </span><span style="color: Tomato;"><strong>key</strong></span><span style="color: Tomato;"> property for this element in iterable.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>&#123;items.map(item =&gt; &lt;li&gt;&#123;item&#125;&lt;/li&gt;)&#125; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The order of the items may change, and having a key can help React identify which item was moved.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Check the </span><span style="color: lightgreen;"><a href="https://react.dev/learn/rendering-lists#why-does-react-need-keys">React documentation</a></span><span style="color: lightgreen;">. </span> </code></pre>

Valid

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

Options

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.

json
{
	"linter": {
		"rules": {
			"correctness": {
				"useJsxKeyInIterable": {
					"options": {
						"checkShorthandFragments": true
					}
				}
			}
		}
	}
}

jsx
data.map((x) => <>{x}</>);
<pre class="language-text"><code class="language-text">code-block.jsx:1:17 <a href="https://biomejs.dev/linter/rules/use-jsx-key-in-iterable">lint/correctness/useJsxKeyInIterable</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Missing </span><span style="color: Tomato;"><strong>key</strong></span><span style="color: Tomato;"> property for this element in iterable.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>data.map((x) =&gt; &lt;&gt;&#123;x&#125;&lt;/&gt;); <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The order of the items may change, and having a key can help React identify which item was moved.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Check the </span><span style="color: lightgreen;"><a href="https://react.dev/learn/rendering-lists#why-does-react-need-keys">React documentation</a></span><span style="color: lightgreen;">. </span> </code></pre> </TabItem> </Tabs>