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

  1. Create an array of items
  2. Use map() to loop through items
  3. Return JSX elements
  4. 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

JUnit Tutorial

How to Master Full Stack Development?

Spring Framework Practice Problems and Their Solutions

30 MCQs on JUnit

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

Java Practice Exercise

programmingempire

Princites

Leave a Reply

Your email address will not be published. Required fields are marked *