noArrayIndexKey (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled βSummaryβ- Rule available since:
v1.0.0 - Diagnostic Category:
lint/suspicious/noArrayIndexKey - 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.
- This rule belongs to the following domains:
- Sources:
- Same as
react/no-array-index-key - Same as
react-x/no-array-index-key - Same as
@eslint-react/no-array-index-key
- Same as
How to configure
Section titled βHow to configureβ{ "linter": { "rules": { "suspicious": { "noArrayIndexKey": "error" } } }}Description
Section titled βDescriptionβDiscourage the usage of Array index in keys.
We donβt recommend using indexes for keys if the order of items may change. This can negatively impact performance and may cause issues with component state. Check out Robin Pokornyβs article for an in-depth explanation on the negative impacts of using an index as a key. If you choose not to assign an explicit key to list items then React will default to using indexes as keys.
Source React documentation
Examples
Section titled βExamplesβInvalid
Section titled βInvalidβsomething.forEach((Element, index) => { <Component key={index} >foo</Component>});code-block.jsx:2:21 lint/suspicious/noArrayIndexKey ββββββββββββββββββββββββββββββββββββββββββββββββ
β Avoid using the index of an array as key property in an element.
1 β something.forEach((Element, index) => {
> 2 β <Component key={index} >foo</Component>
β ^^^^^
3 β });
4 β
βΉ This is the source of the key value.
> 1 β something.forEach((Element, index) => {
β ^^^^^
2 β <Component key={index} >foo</Component>
3 β });
βΉ The order of the items may change, and this also affects performances and component state.
βΉ Check the React documentation.
React.Children.map(this.props.children, (child, index) => ( React.cloneElement(child, { key: index })))code-block.jsx:2:38 lint/suspicious/noArrayIndexKey ββββββββββββββββββββββββββββββββββββββββββββββββ
β Avoid using the index of an array as key property in an element.
1 β React.Children.map(this.props.children, (child, index) => (
> 2 β React.cloneElement(child, { key: index })
β ^^^^^
3 β ))
4 β
βΉ This is the source of the key value.
> 1 β React.Children.map(this.props.children, (child, index) => (
β ^^^^^
2 β React.cloneElement(child, { key: index })
3 β ))
βΉ The order of the items may change, and this also affects performances and component state.
βΉ Check the React documentation.
something.forEach((Element, index) => { <Component key={`test-key-${index}`} >foo</Component>});code-block.jsx:2:33 lint/suspicious/noArrayIndexKey ββββββββββββββββββββββββββββββββββββββββββββββββ
β Avoid using the index of an array as key property in an element.
1 β something.forEach((Element, index) => {
> 2 β <Component key={`test-key-${index}`} >foo</Component>
β ^^^^^
3 β });
4 β
βΉ This is the source of the key value.
> 1 β something.forEach((Element, index) => {
β ^^^^^
2 β <Component key={`test-key-${index}`} >foo</Component>
3 β });
βΉ The order of the items may change, and this also affects performances and component state.
βΉ Check the React documentation.
something.forEach((Element, index) => { <Component key={"test" + index} >foo</Component>});code-block.jsx:2:30 lint/suspicious/noArrayIndexKey ββββββββββββββββββββββββββββββββββββββββββββββββ
β Avoid using the index of an array as key property in an element.
1 β something.forEach((Element, index) => {
> 2 β <Component key={"test" + index} >foo</Component>
β ^^^^^
3 β });
4 β
βΉ This is the source of the key value.
> 1 β something.forEach((Element, index) => {
β ^^^^^
2 β <Component key={"test" + index} >foo</Component>
3 β });
βΉ The order of the items may change, and this also affects performances and component state.
βΉ Check the React documentation.
something.forEach((item) => { <Component key={item.id} >foo</Component>});something.forEach((item) => { <Component key={item.baz.foo} >foo</Component>});Related links
Section titled βRelated linksβCopyright (c) 2023-present Biome Developers and Contributors.