Rendering lists is common in React, but small mistakes can cause bugs or slow apps. Knowing these mistakes helps you write better code.
Common list rendering mistakes in React
function ListComponent({ items }) { return ( <ul> {items.map(item => ( <li key={item.id}>{item.name}</li> ))} </ul> ); }
Always add a unique key prop to each list item to help React track changes.
Keys should be stable and unique, not just array indexes.
id used as the key.const items = [{ id: 1, name: 'Apple' }, { id: 2, name: 'Banana' }]; function FruitList() { return ( <ul> {items.map(item => ( <li key={item.id}>{item.name}</li> ))} </ul> ); }
const items = ['Apple', 'Banana']; function FruitList() { return ( <ul> {items.map((item, index) => ( <li key={index}>{item}</li> ))} </ul> ); }
key prop causes React warnings and can lead to rendering bugs.const items = ['Apple', 'Banana']; function FruitList() { return ( <ul> {items.map(item => ( <li key={item}>{item}</li> // Missing key prop fixed by adding key ))} </ul> ); }
const items = [{ id: 1, name: 'Apple' }, { id: 1, name: 'Banana' }]; function FruitList() { return ( <ul> {items.map(item => ( <li key={item.id}>{item.name}</li> ))} </ul> ); }
This React component shows a list of fruits with unique keys. Clicking the button adds a new fruit with a unique id. This avoids common mistakes like missing keys or duplicate keys.
import React, { useState } from 'react'; function FruitList() { const [fruits, setFruits] = useState([ { id: 1, name: 'Apple' }, { id: 2, name: 'Banana' }, { id: 3, name: 'Cherry' } ]); function addFruit() { // Adding a new fruit with a unique id const newFruit = { id: fruits.length + 1, name: 'Date' }; setFruits([...fruits, newFruit]); } return ( <div> <button onClick={addFruit} aria-label="Add fruit">Add Fruit</button> <ul> {fruits.map(fruit => ( <li key={fruit.id}>{fruit.name}</li> ))} </ul> </div> ); } export default FruitList;
Using array index as key is okay only if the list never changes order or items.
Keys help React update only changed items, improving performance.
Common mistake: forgetting keys or using non-unique keys causes UI bugs and warnings.
Always add a unique and stable key prop when rendering lists.
Avoid using array indexes as keys if the list can change.
Proper keys help React update the UI efficiently and correctly.
Practice
key prop when rendering lists in React?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 DQuick Check:
Keys improve UI updates = A [OK]
- Thinking keys affect styling
- Believing keys prevent rendering
- Assuming keys sort the list
items array?Solution
Step 1: Check key uniqueness and stability
Usingitem.idensures 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 BQuick Check:
Use unique stable keys = B [OK]
- Using array index as key
- Omitting the key prop
- Using non-unique keys
const items = ['apple', 'banana', 'cherry'];
function FruitList() {
return (
<ul>
{items.map((fruit, index) => (
<li key={index}>{fruit}</li>
))}
</ul>
);
}Assuming the list changes by adding 'date' at the start, what problem might occur?
Solution
Step 1: Understand index as key behavior
Using index as key means keys depend on item position, not identity.Step 2: Analyze effect of adding item at start
Adding 'date' at start shifts indexes, causing React to mismatch items and update wrong elements.Final Answer:
React may reuse wrong list items causing UI bugs due to index keys. -> Option AQuick Check:
Index keys cause UI bugs on list changes = D [OK]
- Assuming index keys are always safe
- Thinking React throws error for numeric keys
- Believing list won't render without unique keys
const users = [{id: 1, name: 'Anna'}, {id: 2, name: 'Ben'}];
function UserList() {
return (
<ul>
{users.map(user => <li key={user.name}>{user.name}</li>)}
</ul>
);
}Solution
Step 1: Check key uniqueness
Keys must be unique and stable. Using user.name risks duplicates if names repeat.Step 2: Confirm other code parts
Key prop is present; JSX syntax is correct;- is appropriate for list items.
Final Answer:
Using user.name as key may cause duplicate keys if names repeat. -> Option AQuick Check:
Keys must be unique = C [OK]
- Using non-unique keys like names
- Omitting key prop
- Misunderstanding JSX syntax
const tasks = [
{id: 1, title: 'Task A'},
{id: 2, title: 'Task B'},
{id: 1, title: 'Task C'}
];
function TaskList() {
return (
<ul>
{tasks.map((task, index) => (
<li key={/* What to put here? */}>{task.title}</li>
))}
</ul>
);
}Solution
Step 1: Understand key uniqueness requirement
Keys must be unique and stable. Duplicate IDs break uniqueness.Step 2: Combine id and index for uniqueness
Combining id and index creates unique keys like '1-0', '1-2', avoiding conflicts.Final Answer:
Combine id and index like key={`${task.id}-${index}`} for unique keys. -> Option CQuick Check:
Unique keys from combined values = A [OK]
- Using duplicate IDs as keys
- Omitting keys
- Using only index keys causing UI bugs
Ready to prove it?
Pass the test and this pattern moves unknown
