React List Rendering using map() Function (with Example)
๐ INTRODUCTION
In React, displaying multiple items dynamically is a common task. The map() function is used to render lists efficiently.
It allows you to:
- Loop through arrays
- Generate UI elements dynamically
- Build reusable components
๐ This concept is widely used in React projects and real-world applications.
๐ PROBLEM STATEMENT
๐ Create a program to render a list of items using the map() function in React.
๐ก CONCEPTS USED
- Array
map()method - JSX rendering
- Keys in React
- Functional Components
๐ง LOGIC EXPLANATION
- Create an array of items
- Use
map()to loop through items - Return JSX elements
- Display the list
๐ป PROGRAM CODE
Open src/App.js:
function App() {
const fruits = ["Apple", "Banana", "Mango", "Orange"];
return (
<div>
<h1>Fruit List</h1>
<ul>
{fruits.map((fruit, index) => (
<li key={index}>{fruit}</li>
))}
</ul>
</div>
);
}
export default App;
๐ฅ๏ธ OUTPUT
Fruit List
โข Apple
โข Banana
โข Mango
โข Orange
๐ STEP-BY-STEP EXPLANATION
fruits.map()โ loops through array(fruit, index)โ each item + index<li key={index}>โ required for React list rendering- JSX dynamically creates list items
โ ๏ธ IMPORTANT: KEY IN REACT
React requires a unique key for each list item.
๐ Why?
- Helps React identify elements
- Improves performance
- Prevents UI bugs
โก ADVANCED EXAMPLES
1. Rendering Objects
const students = [
{ id: 1, name: "Juhi" },
{ id: 2, name: "Cherry" }
];
<ul>
{students.map(student => (
<li key={student.id}>{student.name}</li>
))}
</ul>
2. Styling List Items
<li key={index} style={{ color: "blue" }}>
{fruit}
</li>
3. Conditional Rendering
{fruits.map((fruit, index) => (
fruit !== "Mango" && <li key={index}>{fruit}</li>
))}
๐ฏ WHY map() IS IMPORTANT
- Core concept in React
- Used in almost every project
- Helps build dynamic UI
- Essential for API data rendering
๐ REAL-WORLD USE CASE
- Displaying products in e-commerce apps
- Showing user lists
- Rendering API data
- Dashboard components
โ VIVA QUESTIONS (IMPORTANT)
Q1. What is map() in JavaScript?
๐ A method used to iterate over arrays and return a new array.
Q2. Why is map() used in React?
๐ To render lists dynamically in JSX.
Q3. What is a key in React?
๐ A unique identifier for list elements.
Q4. Can we use index as key?
๐ Yes, but not recommended for dynamic lists.
Q5. Does map() modify original array?
๐ No, it returns a new array.
๐ RELATED POSTS
๐ MCA 168 Full Stack Development Lab Programs List
๐ React Hello World โ Program 5
๐ React Counter App โ Program 6
๐ JavaScript Arrow Function โ Program 1
๐ CONCLUSION
List rendering using map() is one of the most important skills in React development.
๐ Master this concept to build real-world applications efficiently.
Further Reading
How to Master Full Stack Development?
Spring Framework Practice Problems and Their Solutions
From Google to the World: The Story of Go Programming Language
Why Go? Understanding the Advantages of this Emerging Language
Creating and Executing Simple Programs in Go
20+ Interview Questions on Go Programming Language
100+ MCQs On Java Architecture
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
