📖 Scenario: You are building a simple React app that shows a list of fruits. Each fruit should be displayed as a separate item.
🎯 Goal: Create a React component that renders a list of fruits using the map() method. Use the key prop correctly to help React identify each item uniquely.
📋 What You'll Learn
Create an array of fruit names
Add a variable for a unique key prefix
Use map() to render each fruit inside a <li> element
Add a unique key prop to each <li> using the prefix and index
Render the list inside a <ul> element
💡 Why This Matters
🌍 Real World
Lists are everywhere in web apps, like menus, product lists, or messages. Using keys helps React update only changed items efficiently.
💼 Career
Understanding keys is essential for React developers to build performant 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
Add a key prefix variable
Add a constant string variable called keyPrefix and set it to 'fruit-' inside the FruitList component, below the fruits array.
React
Hint
Use const keyPrefix = 'fruit-'; to create a prefix for keys.
3
Render the fruit list with keys
Use fruits.map() to create a list of <li> elements inside a <ul>. Use fruit and index as parameters in the map callback. Add a key prop to each <li> with the value {keyPrefix + index}. Display the fruit name inside each <li>.
React
Hint
Use fruits.map((fruit, index) => <li key={keyPrefix + index}>{fruit}</li>) inside a <ul>.
4
Complete the component export
Ensure the FruitList component is exported as the default export and the JSX structure includes the <ul> with the mapped fruit items and keys.
React
Hint
Make sure the component is exported with export default function FruitList() and returns the full JSX with keys.
Practice
(1/5)
1. Why does React require keys when rendering lists with map()?
easy
A. To uniquely identify each list item for efficient updates
B. To style each list item differently
C. To sort the list items automatically
D. To prevent the list from rendering
Solution
Step 1: Understand React's list rendering
React uses keys to track each item in a list so it can update only changed items instead of re-rendering the whole list.
Step 2: Identify the purpose of keys
Keys must be unique and stable to help React identify which items changed, were added, or removed.
Final Answer:
To uniquely identify each list item for efficient updates -> Option A
Quick Check:
Keys = Unique IDs for list items [OK]
Hint: Keys uniquely identify list items for React updates [OK]
Common Mistakes:
Thinking keys are for styling
Believing keys sort the list
Assuming keys prevent rendering
2. Which of the following is the correct way to assign keys in a React list?
easy
A. {items.map(item => <li key={index}>{item.name}</li>)}
B. {items.map(item => <li key={item.id}>{item.name}</li>)}
C. {items.map(item => <li key={Math.random()}>{item.name}</li>)}
D. {items.map(item => <li>{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 or Math.random() can cause unstable keys leading to rendering bugs. Omitting keys causes React warnings.
Final Answer:
{items.map(item => <li key={item.id}>{item.name}</li>)} -> Option B
Quick Check:
Use stable unique IDs as keys [OK]
Hint: Use unique stable IDs, not index or random, for keys [OK]
Common Mistakes:
Using array index as key when list changes
Using random values for keys
Not providing any key
3. What will be the output in the console when this React code runs?