Rules
no-duplicate-key
Prevents duplicate `key` props on sibling elements when rendering lists.
This rule is experimental and may change in the future or be removed. It is not recommended for use in production code at this time.
Full Name in eslint-plugin-react-x
react-x/no-duplicate-keyFull Name in @eslint-react/eslint-plugin
@eslint-react/no-duplicate-keyFeatures
๐งช
Presets
Rule Details
React uses keys to identify elements in an array. If two elements have the same key, React will not be able to distinguish them. This can lead to issues with state and rendering.
Common Violations
Invalid
function MyComponent () {
return [
<li key="1">Item 1</li>
<li key="1">Item 2</li>
// ^^^ The 'key' prop must be unique to its sibling elements.
]
};function MyComponent() {
return (
<ul>
<li key="1">Item 1</li>
<li key="1">Item 2</li>
{/* ^^^ The 'key' prop must be unique to its sibling elements. */}
</ul>
);
}function MyComponent() {
return (
<ul>
{["a", "b"].map((id) => <li key="1">{id}</li>)}
</ul>
);
// ^^^ The 'key' prop must be unique to its sibling elements.
}Valid
function MyComponent () {
return [
<li key="1">Item 1</li>
<li key="2">Item 2</li>
]
};function MyComponent() {
return (
<ul>
<li key="1">Item 1</li>
<li key="2">Item 2</li>
</ul>
);
}function MyComponent() {
return (
<ul>
{["a", "b"].map((id) => <li key={id}>{id}</li>)}
</ul>
);
}Resources
Further Reading
See Also
react-x/no-array-index-key
Disallows using an item's index in the array as its key.react-x/no-implicit-key
Prevents implicitly passing thekeyprop to components.react-x/no-missing-key
Disallows missingkeyon items in list rendering.