Try in Playground
react-best-practicesBest PracticeWarning


No tags


Warns if an element uses an Array index as its key.

The key is used by React to identify which items were added, removed, or updated. This allows React to determine which elements can remain stable between renders.

Using an array index as a key isn't recommended because React can't uniquely identify your elements or components, which can lead to unnecessary renders.


  • You are using an array index as your key for a list of components and you remove a component near the middle of your list.
  • All of your components after that removed element will be re-rendered because their key value has changed.


  • By using a unique identifier for your components like an id or a name you can save yourself and your application from these unnecessary re-renders.

Ast Rule: html element


How to write a rule
function visit(node) {
  if (!node || !node.attributes || node.attributes === 0) return;

  // check if the node has a "key" attribute
  const keyAttribute = node.attributes.find((a) => === "key");
  if (!keyAttribute) return;

  // check the value passed in the attribute
  const keyAttributeValue = keyAttribute?.value?.elements[0].value;
  if (!keyAttributeValue) return;

  const currentFunctionCall = node.context.currentFunctionCall;
  if (!currentFunctionCall) return;

  // check if we're in a map function
  if (!currentFunctionCall.functionName?.name?.value === "map") return;

  // check that we get the arguments of the map function
  if (currentFunctionCall?.arguments?.values?.length !== 1) return;

  const arg = currentFunctionCall.arguments.values[0].value;
  if (arg.astType !== "functionexpression") return;

  // check if the function is using the second parameter - index
  if (arg.parameters?.values.length < 2) return;

  const indexName = arg.parameters.values[1].name.value;

  // if the second argument of map is the same value as the key property, raise an issue
  if (indexName === keyAttributeValue) {
    const error = buildError(
      "Don't use the array index as key",


Expected test result: no error

things.forEach((thing) => (
  <Hello key={} />

things.forEach((thing, index) => (
  <Hello key={} />

things.forEach((thing, index, arr) => (
  <Hello key={arr[index].id} />


Expected test result: has error, i) => (
  <Hello key={i} />
));, idx) => (
  <Hello key={idx} />
));, index) => (
  <Hello key={index} />
));, index, arr) => (
  <Hello key={index} />
Add comment

Log in to add a comment

    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
  • Security
  • Privacy Policy
  • Code Privacy
  • Terms of Service
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.