Rendering Lists with the Array map Method0%

Rendering Lists with the Array map Method

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Rendering Lists with the Array map Method

1. Transforming Data Collections into UI

Modern web applications are built around collections of data: lists of products, feeds of social posts, tables of transactions, or directories of courses. In React, you don't write manual for loops to insert DOM elements. Instead, you use JavaScript's functional Array.prototype.map() method to transform an array of data objects into an array of JSX elements.

Output
Data Array:
['HTML5', 'CSS3', 'JavaScript']
│
▼ .map(item => <li>{item}</li>)
JSX Array:
[<li>HTML5</li>, <li>CSS3</li>, <li>JavaScript</li>]
│
▼ React renders to DOM:
<ul>
<li>HTML5</li>
<li>CSS3</li>
<li>JavaScript</li>
</ul>

2. Basic List Rendering

Here is a straightforward example rendering an array of strings:

JSX
import React from 'react';
 
export default function TechStack() {
const technologies = ['React', 'TypeScript', 'Tailwind CSS', 'Vite'];
 
return (
<div className="tech-container">
<h3>Course Tech Stack</h3>
<ul>
{technologies.map((tech) => (
<li key={tech} className="tech-item">
{tech}
</li>
))}
</ul>
</div>
);
}

3. Rendering Arrays of Objects

In enterprise applications, data almost always arrives from APIs as an array of objects. You can map over these objects and extract their properties cleanly:

JSX
import React from 'react';
 
const COURSES = [
{ id: 'c-101', title: 'React for Beginners', level: 'Beginner', duration: '12h' },
{ id: 'c-102', title: 'Full-Stack Node.js', level: 'Intermediate', duration: '18h' },
{ id: 'c-103', title: 'Django Enterprise API', level: 'Advanced', duration: '24h' }
];
 
export default function CourseDirectory() {
return (
<div className="course-grid">
{COURSES.map((course) => (
<div key={course.id} className="course-card">
<h4>{course.title}</h4>
<p>Level: <span className="badge">{course.level}</span></p>
<span className="duration">⏱ {course.duration}</span>
</div>
))}
</div>
);
}

4. Clean Code: Extracting Child Components

When each list item contains complex markup, multiple event handlers, or local state, inline JSX inside .map() can become difficult to read and maintain.

The industry standard pattern is to extract the item into its own dedicated component:

JSX
// Dedicated child item component
function CourseCard({ course, onEnroll }) {
return (
<div className="course-card">
<h4>{course.title}</h4>
<p>Level: {course.level}</p>
<button onClick={() => onEnroll(course.id)}>Enroll Now</button>
</div>
);
}
 
// Parent list component
export default function CourseList({ courses, onEnrollCourse }) {
return (
<div className="course-list">
{courses.map((course) => (
<CourseCard
key={course.id}
course={course}
onEnroll={onEnrollCourse}
/>
))}
</div>
);
}

Note: The key prop must always be placed directly on the element or component returned inside the .map() callback (here, on <CourseCard key={course.id} />), not inside the child component.


Interactive Quiz

Practice Quiz

Question 1
Which JavaScript array method is the primary tool for rendering lists of elements in React?
Question 2
Why is forEach() NOT used directly inside JSX curly braces to render a list?
Question 3
Where must the key prop be placed when mapping an array to a custom child component?
Question 4
What happens if you pass an empty array [] to .map() in React JSX?
Question 5
What is the primary readability benefit of extracting list items into a separate sub-component?
Next Lesson

The Critical Importance of Unique Keys

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext