In almost every dynamic web application, you need to render collections of repeating data—such as a list of search results, shopping cart items, or table rows. In React, you transform arrays of data into lists of elements using the standard JavaScript Array.prototype.map() method.
Here is how to render an array of strings into an unordered HTML list (<ul>):
function CourseList() {
const courses = [
{ id: 'c1', title: 'Data Structures & Algorithms in C++' },
{ id: 'c2', title: 'React JS & Full Stack Web Dev' },
{ id: 'c3', title: 'Relational Database Management (SQL)' }
];
return (
<div className="course-box">
<h3>BCA Semester Syllabus</h3>
<ul>
{courses.map(course => (
<li key={course.id}>
{course.title}
</li>
))}
</ul>
</div>
);
}
If you forget the key attribute, React will output a red console warning:
Behind the scenes, React's Virtual DOM reconciliation algorithm matches virtual nodes against real DOM elements. When an item is inserted at the top of a 1,000-item list:
c1, c2, and c3 just shifted position, and cleanly inserts only the single new DOM node at the top in O(n) linear time.Beginners often write courses.map((item, index) => <li key={index}>). While this silences the console warning, it causes severe UI bugs when deleting or sorting items:
| Using Stable ID (key={item.id}) | Using Array Index (key={index}) |
|---|---|
|
✓ State stays attached to the exact item. ✓ Checkboxes and text inputs stay with correct record. ✓ CSS transitions and animations render smoothly. |
❌ Deleting row #1 shifts index 1 to 0; wrong item appears deleted. ❌ Input fields retain previous values incorrectly. ❌ Poor reconciliation performance on large lists. |
Now let us explore user input handling and data validation in React Forms & Controlled Inputs.
Accelerate your React mastery by studying these tightly connected core tutorials:
Why stable keys allow O(n) algorithmic DOM updates.
Combining filter(), map(), and conditionals.
Querying paginated list data from REST APIs.