Rendering Lists with map()
Learn how to display multiple items dynamically in React using the JavaScript map() method.
1. Introduction to Rendering Lists
In React, lists are used to display multiple items such as
courses, students, products, and employee names. Instead of
writing separate JSX elements for every item, we can use the
JavaScript map() method to create elements
dynamically from an array.
The map() method visits each item in an array
and returns a new array containing the result of a function
applied to each item.
2. Why Do We Use map()?
Suppose we want to display three course names.
Without map()
function CourseList() {
return (
<ul>
<li>React</li>
<li>Angular</li>
<li>ASP.NET Core</li>
</ul>
);
}
This approach works, but writing every item separately becomes difficult when the array contains many items.
With map()
function CourseList() {
const courses = ["React", "Angular", "ASP.NET Core"];
return (
<ul>
{courses.map((course) => (
<li key={course}>{course}</li>
))}
</ul>
);
}
The map() method automatically creates one list item for each course in the array.
3. How Does map() Work?
The following diagram explains how an array is transformed into React list elements.
4. Syntax of map()
array.map((item) => {
return (
// JSX element
);
});
- array: The source array containing data.
- item: The current item being processed.
- map(): Creates a new array from the returned values.
- return: Returns the value or JSX element for each item.
When an arrow function contains a single JSX expression, parentheses can be used for an implicit return.
{courses.map((course) => (
<li key={course}>{course}</li>
))}
5. Rendering a List of Names
In this example, the names array contains student names. The map() method creates a list item for every name.
Example
function StudentList() {
const students = ["Amit", "Priya", "Rahul", "Sneha"];
return (
<div>
<h2>Student Names</h2>
<ul>
{students.map((student) => (
<li key={student}>{student}</li>
))}
</ul>
</div>
);
}
export default StudentList;
Expected Output
Student Names
- Amit
- Priya
- Rahul
- Sneha
6. Rendering a List of Objects
Arrays can also contain objects. We can use map() to access object properties and display them in JSX.
Example
function CourseList() {
const courses = [
{ id: 1, name: "React" },
{ id: 2, name: "Angular" },
{ id: 3, name: "ASP.NET Core" }
];
return (
<ul>
{courses.map((course) => (
<li key={course.id}>
{course.name}
</li>
))}
</ul>
);
}
export default CourseList;
Expected Output
- React
- Angular
- ASP.NET Core
course.name displays the
course name, while course.id provides a stable
key for each list item.
7. Why Are Keys Used with map()?
When rendering lists, React uses keys to identify items among their siblings. Keys help React match items when the list changes.
Example
{courses.map((course) => (
<li key={course.id}>{course.name}</li>
))}
- Use a unique and stable identifier whenever possible.
- Keys should be unique among sibling elements.
- Avoid generating a new random key on every render.
- The key is used by React and is not available as a regular prop inside the child component.
The next lesson explains keys and unique keys in more detail.
8. Common Mistakes
Mistake 1: Forgetting to return JSX
{courses.map((course) => {
<li key={course}>{course}</li>
})}
With curly braces in an arrow function body, an explicit return is required. Otherwise, the callback returns undefined.
Correct Code
{courses.map((course) => {
return <li key={course}>{course}</li>;
})}
Mistake 2: Missing key
{courses.map((course) => (
<li>{course}</li>
))}
Add a suitable key to each sibling element returned from the map() callback.
Correct Code
{courses.map((course) => (
<li key={course}>{course}</li>
))}
9. Practice Tasks
Complete the following tasks to practise rendering lists with map().
Summary
The map() method is used in React to transform array data into JSX elements and render lists dynamically. It reduces repeated JSX code and makes it easier to display arrays of values or objects. Each item can be accessed through the callback parameter, and object properties can be displayed directly inside JSX. Every sibling element returned from a list should have a suitable key so React can identify items when the list changes.