Consider this React component that renders a list of items:
function ItemList() {
const items = ['apple', 'banana', 'cherry'];
return (
<ul>
{items.map((item) => <li>{item}</li>)}
</ul>
);
}What problem will this cause in the browser console?
function ItemList() { const items = ['apple', 'banana', 'cherry']; return ( <ul> {items.map((item) => <li key={item}>{item}</li>)} </ul> ); }
React needs a way to identify each list item uniquely for efficient updates.
React requires a unique key prop on each list item to track changes efficiently. Without keys, React shows a warning in the console.
This React code tries to render a list but has a syntax error:
const numbers = [1, 2, 3];
return (
<ul>
{numbers.map(number => <li key={number}>{number}<li>)}
</ul>
);Which option fixes the syntax error?
const numbers = [1, 2, 3]; return ( <ul> {numbers.map(number => <li key={number}>{number}</li>)} </ul> );
Check that all JSX tags are properly closed.
The original code misses the closing </li> tag. Option A correctly closes the <li> tag.
Look at this React component:
function TodoList() {
const todos = [
{ id: 1, task: 'Wash dishes' },
{ id: 2, task: 'Do laundry' },
{ id: 3, task: 'Cook dinner' }
];
return (
<ul>
{todos.map((todo, index) => (
<li key={index}>{todo.task}</li>
))}
</ul>
);
}What problem can arise from using index as the key?
function TodoList() { const todos = [ { id: 1, task: 'Wash dishes' }, { id: 2, task: 'Do laundry' }, { id: 3, task: 'Cook dinner' } ]; return ( <ul> {todos.map((todo, index) => ( <li key={index}>{todo.task}</li> ))} </ul> ); }
Think about what happens if the list items reorder or change.
Keys should be stable and unique. Using index as key can cause React to confuse items when the list changes, leading to bugs in UI updates.
Examine this React component:
import { useState } from 'react';
function CounterList() {
const [counts, setCounts] = useState([0, 0, 0]);
function incrementFirst() {
counts[0] += 1;
setCounts(counts);
}
return (
<div>
<ul>
{counts.map((count, i) => <li key={i}>Count {i + 1}: {count}</li>)}
</ul>
<button onClick={incrementFirst}>Increment First</button>
</div>
);
}What happens when the button is clicked?
import { useState } from 'react'; function CounterList() { const [counts, setCounts] = useState([0, 0, 0]); function incrementFirst() { counts[0] += 1; setCounts(counts); } return ( <div> <ul> {counts.map((count, i) => <li key={i}>Count {i + 1}: {count}</li>)} </ul> <button onClick={incrementFirst}>Increment First</button> </div> ); }
React state should not be mutated directly; think about how React detects changes.
Mutating the state array directly and passing the same reference to setState does not trigger React to re-render. The UI stays the same.
Why does React require a unique key for each element in a list?
Think about how React updates the UI efficiently when data changes.
React uses keys to track elements between renders. This helps React update only the changed elements instead of re-rendering the entire list.