📖 Scenario: You are building a simple React app that shows a list of fruits. Each fruit has a unique ID and a name.In React, when you create lists, you need to give each item a special key so React can keep track of them efficiently. This helps React update only the changed items when the list changes, like when you add or remove fruits.
🎯 Goal: Build a React functional component that renders a list of fruits using the map() method. Assign a unique key prop to each list item using the fruit's id.
📋 What You'll Learn
Create a list of fruit objects with id and name properties
Create a React functional component named FruitList
Use the map() method to render each fruit inside an unordered list <ul>
Assign the key prop to each <li> using the fruit's id
💡 Why This Matters
🌍 Real World
React apps often display lists of items like products, messages, or users. Using keys helps React update these lists quickly and correctly.
💼 Career
Understanding keys is essential for React developers to build efficient and bug-free user interfaces that handle dynamic lists.
Progress0 / 4 steps
1
Create the fruit data array
Create a constant array called fruits with these exact objects: { id: 1, name: 'Apple' }, { id: 2, name: 'Banana' }, and { id: 3, name: 'Cherry' }.
React
Hint
Use const fruits = [ ... ] with the exact objects inside.
2
Create the FruitList component
Create a React functional component named FruitList that returns an empty unordered list <ul>.
React
Hint
Define function FruitList() and return an empty <ul> element.
3
Render the fruit names with map()
Inside the FruitList component's <ul>, use fruits.map() with parameters fruit to create a list item <li> for each fruit. Display the fruit's name inside each <li>.
React
Hint
Use fruits.map(fruit => ( ... )) and return <li>{fruit.name}</li>.
4
Add unique key prop to each list item
Add a key prop to each <li> inside the map() using fruit.id as the key.
React
Hint
Add key={fruit.id} inside the <li> tag.
Practice
(1/5)
1. Why does React require a key prop when rendering lists of components?
easy
A. To style each element differently
B. To uniquely identify each element and help React update efficiently
C. To prevent the list from rendering
D. To add event listeners automatically
Solution
Step 1: Understand React's list rendering
React renders lists by creating elements for each item. It needs a way to track each element uniquely.
Step 2: Role of the key prop
The key prop uniquely identifies each element so React can efficiently update or reorder elements without re-rendering the entire list.
Final Answer:
To uniquely identify each element and help React update efficiently -> Option B
Quick Check:
Keys help React track elements = A [OK]
Hint: Keys uniquely identify list items for efficient updates [OK]
Common Mistakes:
Thinking keys are for styling
Believing keys prevent rendering
Assuming keys add event listeners
2. Which of the following is the correct way to assign keys in a React list?
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>)} where index is a variable not defined
D. {items.map(item => <li>{item.name}</li>)} without keys
Solution
Step 1: Check key uniqueness and stability
Keys must be unique and stable. Using item.id is stable and unique for each item.
Step 2: Analyze other options
Using Math.random() generates new keys each render (bad). Using undefined index causes errors. Omitting keys causes React warnings.
Final Answer:
{items.map(item => <li key={item.id}>{item.name}</li>)} -> Option A
Quick Check:
Stable unique keys = A [OK]
Hint: Use stable unique IDs as keys, not random or missing keys [OK]
Common Mistakes:
Using random values for keys
Using undefined variables as keys
Not providing keys at all
3. What will happen if you use the array index as a key when rendering a list that can reorder items?
medium
A. React will ignore the keys and re-render all items
B. React will throw a syntax error
C. React may incorrectly reuse components, causing UI bugs
D. React will optimize rendering perfectly
Solution
Step 1: Understand keys and reordering
Using array index as key works only if list order never changes. If items reorder, keys don't track items correctly.
Step 2: Consequences of using index as key
React may reuse wrong components because keys don't uniquely identify items after reorder, causing UI bugs like wrong data shown.
Final Answer:
React may incorrectly reuse components, causing UI bugs -> Option C
Quick Check:
Index keys cause bugs on reorder = C [OK]
Hint: Avoid index keys if list can reorder or change [OK]
Common Mistakes:
Expecting no bugs with index keys on reorder
Thinking React throws errors for index keys
Assuming React ignores keys if index used
4. Given this code snippet, what is the main problem?