Reiterating why you should never use Array ‘index’ as ‘key’ in your React Applications
Tags: bugs, javascript, arrays, webdev
Published: 2022-10-18 18:00:13

---
title: Reiterating why you should never use Array ‘index’ as ‘key’ in your React Applications
published: true
date: 2022-10-06 19:27:18 UTC
tags: bugs,javascript,arrays,webdevelopment
canonical_url: https://isantoshv.medium.com/array-index-as-key-prop-in-your-react-applications-e219511340d0
---

![reactjs](https://cdn-images-1.medium.com/max/1024/1*cC9PQ00bv1_1CePwcttLhg.jpeg)
_Photo by RealToughCandy.com_

I know you might have heard about passing mandatory key prop in your iterators a lot before, but let's look at it again with an example of a super weird bug 🐞 I found in my code recently.

Imagine you are writing your React code to render a list of items using Array.prototye.map that returns a list of components. If you are working in a Repo with proper eslint setup then you might have got a warning related to react/jsx-key , which means key prop is missing inside your iterator. If you are aware of the rule react/no-array-index-key then you will probably set a unique value to your key prop, else you end up putting the arrayindex as the key prop without understanding the consequences.

Let's look at an example of these two approaches(Using array index and unique id) and reproduce the weird bug that I faced in my code last week. Below is a React codesandbox with the sample of two tables. Both the table components are exactly the same with the same data. The only difference being one table is rendered with array index as key and the other one with a unique id. Additionally, a user can also delete a product row from the table.

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Freact-index-as-key-9h9mtl%3Ffile%3D%2Fsrc%2FApp.js&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2F9h9mtl&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2F9h9mtl%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/e99850d39052f87cf29026ef37e6449e/href">https://medium.com/media/e99850d39052f87cf29026ef37e6449e/href</a></iframe>

Let's try to delete a row in the first table. As soon as you click on theDelete button, the text changes to Deleting... , and the row gets deleted after 3 seconds. However, the button in the next row gets updated toDeleting... , which is weird because we didn’t update it.

However, if you do the same action on the second table, it works perfectly. The only difference between these two tables is the first table rows are rendered with key prop as the index of the products list and the second table rows are rendered with key prop as the id of the product from the products list.

#### Did we fix the bug?

It was weird at first as we were unable to figure out the root cause of the issue. We were thinking of fixing it with a different set of approaches. But once we figured out the actual issue that the key is index by default, it was an easy fix. 🎉

#### Why didn’t my eslint catch this error?

Because, it is not from the code that we wrote. the issue was from a [library](https://github.com/frontend-collective/react-sortable-tree) I use, that sets the default key prop to the index in its iterable. 💭

Apart from this, there are other benefits such as performance optimization if data keeps changing after it is rendered. Keys help React identify which items have changed, are added, or are removed. So it is highly recommended to use unique identifiers as a key prop inside an iterable.

To read more about keys , Click [here](https://reactjs.org/docs/lists-and-keys.html#keys). ⬅️

If your React project doesn’t have the eslint rules react/no-array-index-key ,react/jsx-key enabled, I would highly recommend you enable them right away to avoid any such related UI bugs in the future.

For more information on the other eslint rules that can make you a better developer. Read [here](https://dev.to/devcer/eslint-rules-that-made-me-a-better-react-developer-192k).

Hope this was helpful.

📚 Resources:

- [https://reactjs.org/docs/lists-and-keys.html#keys](https://reactjs.org/docs/lists-and-keys.html#keys)
- [https://robinpokorny.com/blog/index-as-a-key-is-an-anti-pattern/](https://robinpokorny.com/blog/index-as-a-key-is-an-anti-pattern/)
- [https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-key.md](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-key.md)
- [https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-array-index-key.md](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-array-index-key.md)
- [https://isantoshv.medium.com/eslint-rules-that-made-me-a-better-react-developer-7689f0cb443a](https://dev.to/devcer/eslint-rules-that-made-me-a-better-react-developer-192k)