Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

ESLint react/no-array-index-key: Do not use Array index in keys

ErrorDo not use Array index in keys

Short answer

The rule reports key={index} because an index changes when items are added, removed or reordered, so React can match the wrong component to the wrong data. Use a stable value from the data. If items have none, create an ID when the item is created, not during render.

  • Applies to React 19, eslint-plugin-react 7, ESLint 9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run ESLint on the file and read which expression the rule points at.

    npx eslint src/components/List.jsx
  2. 2Match what you see, then jump to its fix

    1. 1Items come from an API or database and carry id, uuid or slug.The data already has an identifierFix
    2. 2The list comes from a form, a local array or a file the user adds to.Items are created in the browser and have no IDFix
    3. 3The key looks like `row-${i}` and is still flagged.The key is built from the index in a template string or cloneElementFix

What does the error look like?

  • Do not use Array index in keys

Cause 1 of 3

The data already has an identifier

1Check

React identifies items by key. A database ID is unique by nature, so the React docs name it as the key to use.

grep -rn "key={" src | grep -i "index\|idx\|i}"

2Fix

Use that value as the key on the outermost element returned by map.

{items.map((item) => (
  <li key={item.id}>{item.name}</li>
))}

3Verify

Run the linter on the file again.

npx eslint src/components/List.jsx

Confirmed by react.dev

Did this fix it?

Cause 2 of 3

Items are created in the browser and have no ID

1Check

The React docs say to give locally created data a key when it is created: an incrementing counter, crypto.randomUUID() or a package such as uuid. Keys made while rendering, like Math.random(), change on every render.

grep -rn "Math.random\|randomUUID" src

2Fix

Assign the ID in the code that adds the item and render with it.

setTodos((prev) => [...prev, { id: crypto.randomUUID(), text }]);

{todos.map((todo) => <li key={todo.id}>{todo.text}</li>)}

3Verify

Add, reorder and remove items, then check that inputs keep their values.

npx eslint src

Confirmed by react.dev

Did this fix it?

Cause 3 of 3

The key is built from the index in a template string or cloneElement

1Check

The rule also reports template literals that contain the index and cloneElement calls with an index key, and index parameters in callbacks such as forEach, filter and reduce.

grep -rn "key={`" src

2Fix

Build the key from a unique property of the item instead of its position.

{rows.map((row) => <Row key={`row-${row.id}`} {...row} />)}

3Verify

Run the linter again.

npx eslint src

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Put an id field in every list item type, API response and test fixture.
  • Use index keys only for static lists that never reorder, as the React docs allow, and say so in a comment.
  • Keep the rule at error level so new lists are written with stable keys.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.