Lint Rules

noArrayIndexKey (since v0.10.0)

This rule is recommended by Rome.

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

Invalid

something.forEach((Element, index) => {
    <Component key={index} >foo</Component>
});
correctness/noArrayIndexKey.js:2:21 lint/correctness/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 })
))
correctness/noArrayIndexKey.js:2:38 lint/correctness/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.