Rendering Arrays with .map()
0 105
🧩 Rendering Arrays with .map() in React
One of the most common tasks in React is displaying a list of items — like blog posts, products, or user profiles. React doesn’t provide a built-in loop like for
or foreach
in JSX, but it works beautifully with JavaScript’s .map()
method.
In this blog, we'll walk through how to render arrays using .map()
in JSX and highlight best practices (like using keys
🔑).
🔁 What Is .map() in JavaScript?
.map()
is a JavaScript method that creates a new array by applying a function to each element of an existing array.
const numbers = [1, 2, 3];
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6]
In React, we use .map()
to loop through an array and return JSX for each item.
📦 Basic Example: Displaying a List
const fruits = ["🍎 Apple", "🍌 Banana", "🍊 Orange"];
const FruitList = () => {
return (
<ul>
{fruits.map((fruit, index) => (
<li key={index}>{fruit}</li>
))}
</ul>
);
};
This code takes each fruit and returns a <li>
element to be displayed in a list. Simple and effective!
🔑 Adding Unique Keys
React requires a key
prop when rendering lists. This helps React identify which items changed, were added, or removed — improving performance and preventing bugs.
const tasks = [
{ id: 1, title: "Learn React" },
{ id: 2, title: "Build a project" }
];
const TaskList = () => {
return (
<ul>
{tasks.map((task) => (
<li key={task.id}>{task.title}</li>
))}
</ul>
);
};
✅ Always use a unique key from your data (like id
) — avoid using array indexes unless the list is static and doesn't change.
🧠 JSX Inside map()
You can return entire components inside a .map()
too!
const users = [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" }
];
const UserCard = ({ name }) => <div className="card">👤 {name}</div>;
const UserList = () => {
return (
<div>
{users.map(user => (
<UserCard key={user.id} name={user.name} />
))}
</div>
);
};
This pattern is commonly used for rendering dynamic UIs from APIs or JSON data.
🧹 Conditional Rendering While Mapping
You can also use conditions inside .map()
to render only certain items:
{users.map(user => (
user.isActive && <p key={user.id}>Active: {user.name}</p>
))}
Use logical checks like &&
or ternary ( ? : )
operators for cleaner output.
⚠️ Avoiding Common Pitfalls
- 🚫 Don’t forget the
key
prop - 🚫 Avoid putting complex logic directly inside JSX — extract into functions if needed
- 🚫 Don’t mutate the original array — always return new JSX safely
✅ Best Practices
- Use meaningful keys (like IDs)
- Keep
.map()
functions clean and readable - Wrap mapped content in a parent element like
<ul>
,<div>
, or React Fragments - Use components inside map when the logic is complex
🧠 Recap
.map()
is used to render arrays of data in JSX- Each element should have a
key
for performance and stability - You can render text, JSX elements, or even components
- Be cautious with conditionals and index-based keys
Rendering lists with .map() is a fundamental pattern in React. Master it, and you’ll unlock the power to build dynamic and flexible interfaces! 🔓
🚀 What’s Next?
- Explore React Fragments to avoid unnecessary wrapping tags
- Learn about Dynamic Component Rendering
- Dive into Filtering and Sorting Lists in React
With .map()
in your toolkit, you’re ready to dynamically build UI from any dataset. Start mapping your way to better React apps! 🗺️
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments