Lists and Keys in React
Learn how to display multiple items in React and understand why keys are important when rendering lists.
1. What Are Lists in React?
A list is a collection of related items. In React, lists are commonly used to display multiple courses, students, products, employees, or other records in a component.
Instead of writing separate JSX elements for every item,
we can store the data in an array and use JavaScript's
map() method to create React elements from it.
map() helps create a JSX element for each item
in an array.
2. Why Do We Use Lists?
Consider a training institute that offers several courses. We want to display all the course names on one page. Lists help us manage and display these items without repeating the same JSX structure.
function CourseList() {
const courses = [
"React",
"ASP.NET Core MVC",
"SQL Server",
"Software Testing"
];
return (
<div>
<h2>CIIT Training Institute Courses</h2>
<ul>
{courses.map((course) => (
<li key={course}>{course}</li>
))}
</ul>
</div>
);
}
export default CourseList;
Code Explanation
-
The
coursesarray stores the course names. -
The
map()method processes each course. - Each course is displayed inside an HTML list item using JSX.
-
The
keyprop helps React identify each item in the rendered list.
Expected Output
CIIT Training Institute Courses
- React
- ASP.NET Core MVC
- SQL Server
- Software Testing
3. How Lists Work in React
The following diagram explains how an array is converted into a list of React elements.
Processes each array item
- React
- SQL Server
- ASP.NET Core
4. What Is the map() Method?
The map() method creates a new array by calling
a function for each item in an existing array. In React,
that function commonly returns JSX elements.
function Students() {
const students = ["Amit", "Priya", "Rahul"];
const studentItems = students.map((student) => (
<li key={student}>{student}</li>
));
return (
<div>
<h2>Student Names</h2>
<ul>{studentItems}</ul>
</div>
);
}
export default Students;
Expected Output
Student Names
- Amit
- Priya
- Rahul
map() method returns a new array.
It does not directly modify the original array.
5. What Are Keys in React?
A key is a special prop that helps React identify which item in a list has changed, been added, or been removed. Keys should be unique among the sibling elements in a list.
function KeyExample() {
const courses = [
{ id: 101, name: "React" },
{ id: 102, name: "SQL Server" },
{ id: 103, name: "ASP.NET Core MVC" }
];
return (
<ul>
{courses.map((course) => (
<li key={course.id}>
{course.name}
</li>
))}
</ul>
);
}
export default KeyExample;
Code Explanation
- Each course object has a unique
id. -
key={course.id}provides a stable identity for each list item. - React uses keys when reconciling list elements between renders.
Expected Output
- React
- SQL Server
- ASP.NET Core MVC
6. Choosing the Right Key
Use a stable and unique identifier from your data whenever possible. A database ID or a unique record ID is usually a good choice.
| Key Type | Example | Recommendation |
|---|---|---|
| Unique ID | key={course.id} |
Preferred when IDs are stable and unique. |
| Unique string | key={course.code} |
Suitable if the value is unique and stable. |
| Array index | key={index} |
Avoid for lists that can be reordered, inserted into, or deleted from. |
7. Common Mistakes to Avoid
1. Forgetting the key prop
Not recommended:
{courses.map((course) => (
<li>{course.name}</li>
))}
Correct:
{courses.map((course) => (
<li key={course.id}>{course.name}</li>
))}
2. Using a value that is not unique
If two records use the same key, React cannot reliably distinguish them in the list. Use a unique identifier for every sibling item.
8. Practice Tasks
Complete these exercises using React:
-
Create an array of five course names and display them
using
map(). -
Create a list of student names and render each name
inside an
lielement. - Create an array of course objects with unique IDs and display the course names.
- Create a product list where every item uses its product ID as the key.
- Explain why stable, unique keys are important when list items are added, removed, or reordered.
map() to render sibling JSX
elements, check that every element has an appropriate key.
Summary
Lists in React allow multiple items to be displayed from an array. The map() method is commonly used to transform array items into JSX elements. Each rendered item should have a suitable key so React can identify it between renders. Stable and unique IDs are usually the best choice for keys. Understanding lists and keys helps developers create organized, maintainable React components.