📖 Scenario: You are building a simple React app that shows a list of fruits. You want to display each fruit name on the page.
🎯 Goal: Build a React functional component that renders a list of fruit names using the map() method.
📋 What You'll Learn
Create an array of fruit names
Create a variable to hold the list items
Use map() to transform the array into JSX list elements
Render the list inside a <ul> element
💡 Why This Matters
🌍 Real World
Rendering lists is a common task in React apps to display collections like menus, user comments, or product lists.
💼 Career
Understanding how to render lists with keys is essential for React developers to build efficient and bug-free user interfaces.
Progress0 / 4 steps
1
Create the fruits array
Create a constant array called fruits with these exact string values: 'Apple', 'Banana', 'Cherry', 'Date', 'Elderberry'.
React
Hint
Use const fruits = ['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry'];
2
Create the listItems variable
Create a constant called listItems and set it to null for now.
React
Hint
Write const listItems = null;
3
Use map() to create list items
Change the listItems constant to use fruits.map() to create an array of <li> elements. Use fruit as the iterator variable. Each <li> should have a key attribute set to the fruit name and display the fruit name inside.
React
Hint
Use fruits.map(fruit => <li key={fruit}>{fruit}</li>)
4
Render the list inside a component
Create a React functional component called FruitList that returns a <ul> element containing the listItems. Export the component as default.
React
Hint
Write a function FruitList that returns <ul>{listItems}</ul> and export it.
Practice
(1/5)
1. What is the main purpose of using the map() method when rendering lists in React?
easy
A. To filter out unwanted items from the list
B. To sort the list items alphabetically
C. To transform an array of data into an array of React elements
D. To add styles to each list item
Solution
Step 1: Understand the role of map() in React
The map() method is used to take an array of data and create a new array by transforming each item. In React, this means turning data into React elements.
Step 2: Identify what map() does in rendering lists
When rendering lists, map() creates a React element for each item, which React can then display on the screen.
Final Answer:
To transform an array of data into an array of React elements -> Option C
Quick Check:
map() creates elements from data [OK]
Hint: Remember: map() turns data into elements [OK]
Common Mistakes:
Thinking map() sorts or filters data
Using map() without returning elements
Confusing map() with forEach()
2. Which of the following is the correct way to add a unique key to list items in React?
easy
A. {items.map(item => <li key={item.id}>{item.name}</li>)}
B. {items.map(item => <li key={Math.random()}>{item.name}</li>)}
C. {items.map(item => <li key="index">{item.name}</li>)}
D. {items.map(item => <li>{item.name}</li>)}
Solution
Step 1: Understand the importance of keys in React lists
Keys help React identify which items changed, added, or removed. They must be unique and stable for each list item.
Step 2: Evaluate each option for key usage
{items.map(item => <li>{item.name}</li>)} has no key, so React will warn. {items.map(item => <li key={item.id}>{item.name}</li>)} uses a unique id from the item, which is correct. {items.map(item => <li key="index">{item.name}</li>)} uses a string 'index' for all keys, which is not unique. {items.map(item => <li key={Math.random()}>{item.name}</li>)} uses Math.random(), which changes every render and is unstable.
Final Answer:
{items.map(item => <li key={item.id}>{item.name}</li>)} -> Option A