What are keys in React?

Deepak Mankotia
3 min readApr 5, 2023

--

In React, “keys” are special attributes that are used to uniquely identify elements in an array of components or elements that are rendered dynamically. When rendering lists or collections of components in React, each component in the list must have a unique key prop assigned to it.

Keys serve as hints to React about the identity of each component in a list, allowing React to efficiently update the DOM when the list changes. When new elements are added or existing elements are removed or re-ordered in the list, React can use the keys to determine which components need to be created, updated, or deleted, instead of re-rendering the entire list.

The key prop is added to the top-level element of each component in the list, and it should be a unique value among siblings, but not necessarily globally unique. It’s common to use an ID or an index from the array as the key value. However, using the array index as the key is not recommended when the list can change order, as it can cause issues with component state and props.

Here’s an example of using keys in a list of components in React:

const items = [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
{ id: 3, name: 'Item 3' }
];

const itemList = items.map(item => (
<Item key={item.id} name={item.name} />
));

function MyComponent() {
return (
<div>
{itemList}
</div>
);
}

In this example, each item in the items array is mapped to a Item component, and the key prop is set to the id of each item. This allows React to efficiently update the DOM when the list of items changes. Note that the key prop should be unique among siblings, so using the id of each item is a good choice as long as the id values are unique within the list. However, if the id values are not unique, you can use other unique values such as UUIDs or generate unique keys using a library or custom logic. It's important to choose appropriate keys to ensure efficient rendering and smooth behaviour when working with dynamic lists in React.

const items = [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
{ id: 3, name: 'Item 3' }
];

const itemList = items.map(item => (
<Item key={item.id} name={item.name} />
));

function MyComponent() {
return (
<div>
{itemList}
</div>
);
}

In this example, each item in the items array is mapped to a Item component, and the key prop is set to the id of each item. This allows React to efficiently update the DOM when the list of items changes. Note that the key prop should be unique among siblings, so using the id of each item is a good choice as long as the id values are unique within the list. However, if the id values are not unique, you can use other unique values such as UUIDs or generate unique keys using a library or custom logic. It's important to choose appropriate keys to ensure efficient rendering and smooth behaviour when working with dynamic lists in React.

const items = [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item

--

--

Deepak Mankotia
Deepak Mankotia

Written by Deepak Mankotia

Full Stack Web Developer | Open Source Enthusiast

No responses yet