Keys help React keep track of items in a list so it can update the screen efficiently.
Keys concept in React
<ul>
{items.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>The key must be a unique and stable value for each item.
Do not use array index as key if the list can change order or items.
id from each fruit as the key.const fruits = [{id: 'a', name: 'Apple'}, {id: 'b', name: 'Banana'}]; return ( <ul> {fruits.map(fruit => ( <li key={fruit.id}>{fruit.name}</li> ))} </ul> );
const numbers = [10, 20, 30]; return ( <ul> {numbers.map((num, index) => ( <li key={index}>{num}</li> ))} </ul> );
const tasks = [{id: 1, title: 'Clean'}, {id: 2, title: 'Cook'}]; return ( <div> {tasks.map(task => ( <p key={task.id}>{task.title}</p> ))} </div> );
This component shows a list of todo items. Each item has a unique id used as the key. This helps React update the list efficiently if items change.
import React, { useState } from 'react'; export default function TodoList() { const [todos, setTodos] = useState([ { id: 't1', text: 'Learn React' }, { id: 't2', text: 'Build App' }, { id: 't3', text: 'Deploy' } ]); return ( <section> <h2>My Todo List</h2> <ul> {todos.map(todo => ( <li key={todo.id}>{todo.text}</li> ))} </ul> </section> ); }
Always use a unique and stable key to avoid unexpected UI bugs.
Keys help React identify which items changed, so it can update only those parts.
Using array index as key can cause problems if the list changes order or items.
Keys are unique IDs for list items in React.
They help React update lists efficiently and correctly.
Always use stable and unique keys, not array indexes if the list can change.
Practice
map()?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 AQuick Check:
Keys = Unique IDs for list items [OK]
- Thinking keys are for styling
- Believing keys sort the list
- Assuming keys prevent rendering
Solution
Step 1: Check key uniqueness and stability
Usingitem.idensures each key is unique and stable across renders.Step 2: Evaluate other options
UsingindexorMath.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 BQuick Check:
Use stable unique IDs as keys [OK]
- Using array index as key when list changes
- Using random values for keys
- Not providing any key
const items = [
{ id: 1, name: 'Apple' },
{ id: 2, name: 'Banana' },
{ id: 3, name: 'Cherry' }
];
const list = items.map(item => <li key={item.id}>{item.name}</li>);
console.log(list.length);Solution
Step 1: Understand the map function output
Themapcreates an array of 3<li>React elements, one for each item.Step 2: Check the length of the array
The arraylisthas 3 elements, solist.lengthis 3.Final Answer:
3 -> Option AQuick Check:
Array length equals number of items mapped [OK]
- Confusing React elements with strings
- Expecting undefined from map
- Thinking keys affect array length
const items = ['A', 'B', 'C'];
return (
<ul>
{items.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
);Solution
Step 1: Analyze key usage
Using array index as key works only if list never changes order or items.Step 2: Understand potential bugs
If items are added, removed, or reordered, React may reuse wrong elements causing UI bugs.Final Answer:
Using index as key can cause bugs if list changes -> Option CQuick Check:
Index keys cause bugs on list changes [OK]
- Thinking index keys are always safe
- Believing missing keys cause syntax errors
- Assuming map syntax is wrong
const users = [
{ id: 1, name: 'Sam' },
{ id: 2, name: 'Alex' },
{ id: 3, name: 'Sam' }
];
return (
<ul>
{users.map(user => <li key={user.name}>{user.name}</li>)}
</ul>
);What problem might occur and how to fix it?
Solution
Step 1: Identify duplicate keys
Usinguser.nameas key causes duplicates because 'Sam' appears twice.Step 2: Fix by using unique stable keys
Useuser.idwhich is unique for each user to avoid React key conflicts.Final Answer:
Duplicate keys from same names cause rendering bugs; use user.id as key -> Option DQuick Check:
Keys must be unique; use unique IDs [OK]
- Assuming keys can be duplicated
- Thinking keys must be numeric only
- Trying to fix keys by wrapping in fragments
Ready to prove it?
Pass the test and this pattern moves unknown
