📖 Scenario: You are building a simple React component that shows a list of fruits on a webpage. This is a common task in web apps where you want to display multiple items dynamically.
🎯 Goal: Create a React functional component that renders a list of fruits correctly. You will learn how to set up the data, configure a key for each list item, render the list using map(), and complete the component structure to avoid common mistakes.
📋 What You'll Learn
Create a list of fruits as an array of strings
Add a unique key for each list item when rendering
Use the map() method to render the list inside JSX
Complete the React component with proper return and export
💡 Why This Matters
🌍 Real World
Rendering lists is a very common task in web apps, such as showing products, messages, or user comments. Using keys correctly helps React update the UI efficiently.
💼 Career
Understanding list rendering and keys is essential for React developers to build performant and bug-free user interfaces.
Progress0 / 4 steps
1
DATA SETUP: 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"] to create the array.
2
CONFIGURATION: Prepare a unique key for each fruit
Create a function called getKey that takes two parameters: fruit and index. It should return a string combining fruit and index separated by a dash, like "Apple-0".
React
Hint
Use a template string to combine fruit and index with a dash.
3
CORE LOGIC: Render the fruits list with keys using map()
Create a React functional component called FruitList. Inside its return statement, render an unordered list <ul>. Use fruits.map() with parameters fruit and index to create list items <li>. Each <li> must have a key attribute set by calling getKey(fruit, index). The content of each <li> is the fruit string.
React
Hint
Use parentheses around JSX and return the <ul> with mapped <li> elements having keys.
4
COMPLETION: Export the FruitList component
Add the line export default FruitList at the end of the file to export the component.
React
Hint
Use export default FruitList to make the component usable in other files.
Practice
(1/5)
1. Why is it important to add a unique key prop when rendering lists in React?
easy
A. It automatically sorts the list alphabetically.
B. It makes the list items look prettier on the screen.
C. It prevents the list from rendering at all.
D. It helps React identify which items have changed, added, or removed.
Solution
Step 1: Understand the role of keys in React lists
Keys help React track each item uniquely when the list changes.
Step 2: Identify the effect of keys on UI updates
With keys, React can efficiently update only changed items, improving performance and correctness.
Final Answer:
It helps React identify which items have changed, added, or removed. -> Option D
Quick Check:
Keys improve UI updates = A [OK]
Hint: Keys uniquely identify list items for React updates [OK]
Common Mistakes:
Thinking keys affect styling
Believing keys prevent rendering
Assuming keys sort the list
2. Which of the following is the correct way to add keys when rendering a list of items stored in items array?
easy
A. {items.map((item, index) => <li key={index}>{item}</li>)}
B. {items.map(item => <li key={item.id}>{item.name}</li>)}
C. {items.map(item => <li>{item.name}</li>)}
D. {items.map((item, i) => <li key={i + 1}>{item.name}</li>)}
Solution
Step 1: Check key uniqueness and stability
Using item.id ensures each key is unique and stable across renders.
Step 2: Evaluate other options
Using index as key (options A and D) can cause bugs if list changes; no key ({items.map(item => <li>{item.name}</li>)}) causes warnings.
Final Answer:
{items.map(item => <li key={item.id}>{item.name}</li>)} -> Option B
Quick Check:
Use unique stable keys = B [OK]
Hint: Use unique IDs, not indexes, as keys [OK]
Common Mistakes:
Using array index as key
Omitting the key prop
Using non-unique keys
3. What will be the output of this React code snippet?