ESLint react/no-array-index-key: Do not use Array index in keys
Do not use Array index in keysShort 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.
Fix it fast
1Find out which cause you have
Run ESLint on the file and read which expression the rule points at.
npx eslint src/components/List.jsx2Match what you see, then jump to its fix
- 1
Items come from an API or database and carry id, uuid or slug.The data already has an identifierFix - 2
The 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
The key looks like `row-${i}` and is still flagged.The key is built from the index in a template string or cloneElementFix
- 1
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.jsxConfirmed by react.dev
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" src2Fix
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 srcConfirmed by react.dev
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={`" src2Fix
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 srcConfirmed by github.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix