Keys in Lists
Learn how React keys identify list items and help React update lists efficiently.
1. Introduction to Keys
In React, keys are special attributes used when rendering lists of elements. A key helps React identify which item has been added, removed, or changed between renders.
When we use the map() method to render a list, we should provide a suitable key for each sibling element.
2. Why Are Keys Needed?
Suppose a list contains three courses. Later, a new course is added or an existing course is removed. React needs a reliable way to match the items in the updated list with the items from the previous render.
Keys provide that identity and help React determine which list elements need to be updated.
Without a Key
function CourseList() {
const courses = ["React", "Angular", "ASP.NET Core"];
return (
<ul>
{courses.map((course) => (
<li>{course}</li>
))}
</ul>
);
}
This code does not provide a key for each list item. React will show a warning in the development console.
With a Key
function CourseList() {
const courses = ["React", "Angular", "ASP.NET Core"];
return (
<ul>
{courses.map((course) => (
<li key={course}>{course}</li>
))}
</ul>
);
}
In this example, each course name is used as its key. This is suitable because the course names in this array are unique and stable.
3. How Do Keys Work?
The following diagram illustrates how React uses keys to match list items when a list changes.
4. Using Object IDs as Keys
When an array contains objects, a unique ID property is usually a good choice for the key.
Example
function StudentList() {
const students = [
{ id: 101, name: "Amit" },
{ id: 102, name: "Priya" },
{ id: 103, name: "Rahul" }
];
return (
<ul>
{students.map((student) => (
<li key={student.id}>
{student.name}
</li>
))}
</ul>
);
}
export default StudentList;
Expected Output
- Amit
- Priya
- Rahul
Here, student.id identifies each student,
while student.name is displayed on the page.
5. Keys Must Be Unique Among Siblings
Keys should be unique among the elements rendered together in the same list. Duplicate keys can make it difficult for React to match items correctly.
Incorrect Example
const students = [
{ id: 1, name: "Amit" },
{ id: 1, name: "Priya" }
];
{students.map((student) => (
<li key={student.id}>{student.name}</li>
))}
Both students use the same key, so the keys are not unique among these sibling list items.
Correct Example
const students = [
{ id: 1, name: "Amit" },
{ id: 2, name: "Priya" }
];
{students.map((student) => (
<li key={student.id}>{student.name}</li>
))}
Each student now has a distinct ID, so React can use the keys to distinguish the list items.
6. Keys Are Not Regular Props
React uses the key internally to identify elements. The key is not passed to the child component as a regular prop. If the component needs the ID, pass it separately.
Example
function Student({ id, name }) {
return (
<p>
{id} - {name}
</p>
);
}
function StudentList() {
const students = [
{ id: 101, name: "Amit" },
{ id: 102, name: "Priya" }
];
return (
<div>
{students.map((student) => (
<Student
key={student.id}
id={student.id}
name={student.name}
/>
))}
</div>
);
}
Expected Output
101 - Amit
102 - Priya
7. Common Mistakes
Mistake 1: Using Duplicate Keys
{students.map((student) => (
<li key="student">{student.name}</li>
))}
Every item receives the same key. Use a unique identifier for each item instead.
Mistake 2: Using Array Index for a Changing List
{students.map((student, index) => (
<li key={index}>{student.name}</li>
))}
Index keys can cause incorrect item matching when items are inserted, deleted, or reordered. Prefer stable IDs when the data provides them.
Mistake 3: Expecting key as a Prop
<Student key={student.id} name={student.name} />
The Student component cannot read the key through
props.key. Pass a separate prop if the ID
is needed inside the component.
8. Best Practices for Keys
- Use unique, stable IDs from your data whenever possible.
- Ensure keys are unique among sibling elements.
- Avoid generating random keys during every render.
- Avoid array indexes when list items can change order or be inserted or removed.
- Remember that keys are not passed to components as regular props.
9. Practice Tasks
Complete these tasks to practise using keys in React lists.
Summary
Keys are special attributes used when rendering lists in React. They help React identify items between renders when elements are added, removed, or reordered. Keys should be unique among sibling elements and should remain stable between renders. Unique IDs from data are generally a good choice, while array indexes should be avoided for lists that can change. Keys are used internally by React and are not available as regular props inside child components.