Unique Keys in React
Learn why keys must be unique among sibling elements when rendering lists in React.
1. Introduction to Unique Keys
When React renders a list using the map() method, each sibling element should have a key. These keys help React identify individual items when a list changes.
Keys must be unique among sibling elements. If multiple elements in the same list use the same key, React may not correctly match those items during updates.
2. Why Are Unique Keys Important?
Consider a list of students. If two students have the same key, React cannot reliably distinguish those sibling items by their keys.
Unique keys help React:
- Identify individual items in a list.
- Match items between previous and updated renders.
- Handle inserted, removed, and reordered items more reliably.
- Avoid duplicate-key warnings in the development console.
3. Understanding Unique Keys
The diagram shows the difference between duplicate keys and unique keys in the same list.
Duplicate Keys
Duplicate key
Unique Keys
Unique sibling keys
4. Incorrect Example: Duplicate Keys
In this example, both student objects have the same ID. Therefore, using student.id as the key produces duplicate keys in the list.
function StudentList() {
const students = [
{ id: 1, name: "Amit" },
{ id: 1, name: "Priya" },
{ id: 2, name: "Rahul" }
];
return (
<ul>
{students.map((student) => (
<li key={student.id}>
{student.name}
</li>
))}
</ul>
);
}
What Is the Problem?
- Amit and Priya both use key 1.
- The keys are not unique among sibling list items.
- React can display a duplicate-key warning in development.
5. Correct Example: Unique Keys
Assign each student a distinct ID and use that ID as the key.
function StudentList() {
const students = [
{ id: 1, name: "Amit" },
{ id: 2, name: "Priya" },
{ id: 3, name: "Rahul" }
];
return (
<ul>
{students.map((student) => (
<li key={student.id}>
{student.name}
</li>
))}
</ul>
);
}
export default StudentList;
Expected Output
Student List
- Amit
- Priya
- Rahul
Each student has a distinct ID, so each sibling list item receives a unique key.
6. Example: Unique Keys for Courses
The same rule applies when rendering course objects. Each course should have a stable, unique identifier.
function CourseList() {
const courses = [
{ id: 101, name: "React" },
{ id: 102, name: "Angular" },
{ id: 103, name: "ASP.NET Core" }
];
return (
<div>
<h2>CIIT Training Courses</h2>
<ul>
{courses.map((course) => (
<li key={course.id}>
{course.name}
</li>
))}
</ul>
</div>
);
}
export default CourseList;
Expected Output
CIIT Training Courses
- React
- Angular
- ASP.NET Core
7. How to Choose a Unique Key
A key should identify an item consistently between renders. A unique ID from the data is generally the preferred choice.
| Key Choice | Recommendation |
|---|---|
| Unique database ID | Preferred when stable and available |
| Stable unique data ID | Good choice |
| Array index | Avoid for lists that can change order or size |
| Random value generated on each render | Avoid |
| Duplicate ID within the same list | Incorrect |
8. Common Mistakes
Mistake 1: Reusing the Same Key
{students.map((student) => (
<li key="student">{student.name}</li>
))}
Every list item receives the same key. Use a distinct stable identifier for each student.
Mistake 2: Generating Random Keys
{students.map((student) => (
<li key={Math.random()}>{student.name}</li>
))}
A new random key can be generated on each render, causing React to treat existing items as different elements. Prefer stable IDs.
Mistake 3: Assuming Keys Must Be Globally Unique
Keys only need to be unique among sibling elements. Separate lists can reuse the same key values without violating this rule.
9. Practice Tasks
Complete these tasks to practise unique keys in React.
Summary
Unique keys help React identify individual items when rendering lists. Each key must be unique among sibling elements and should remain stable between renders. Unique IDs from the data are generally a good choice. Duplicate keys can cause development warnings and unreliable item matching, while random keys can make React recreate items unnecessarily. Keys do not need to be globally unique across the entire application.