Keys help React identify which items have changed, been added, or removed. This makes updating the UI faster and more efficient.
Click to reveal answer
beginner
Why should keys be unique among siblings in a list?
Unique keys prevent confusion when React compares elements. If keys are not unique, React might update the wrong item, causing bugs.
Click to reveal answer
intermediate
What can happen if you use the array index as a key in React?
Using array index as a key can cause problems when the list changes order or items are added/removed. React may reuse elements incorrectly, leading to UI bugs.
Click to reveal answer
beginner
How do keys improve React's rendering performance?
Keys let React quickly find which elements changed without re-rendering the entire list. This reduces work and speeds up updates.
Click to reveal answer
intermediate
Can two different lists share the same keys for their items?
Yes, keys only need to be unique among siblings in the same list. Different lists can reuse keys without issues.
Click to reveal answer
Why does React require keys for list items?
ATo style the list items differently
BTo track which items changed for efficient updates
CTo add animations automatically
DTo sort the list alphabetically
✗ Incorrect
Keys help React identify changes in lists so it can update only what is needed.
What is a bad practice when assigning keys in React?
AUsing unique IDs from data
BUsing strings as keys
CUsing keys only among siblings
DUsing array index as key when list changes
✗ Incorrect
Using array index as key can cause bugs if the list order changes or items are added/removed.
Keys must be unique among which elements?
ASibling elements in the same list
BAll lists in the app
CAll components in the app
DParent components only
✗ Incorrect
Keys only need to be unique among siblings in the same list.
What happens if keys are missing in a React list?
AReact updates the list less efficiently and may cause bugs
BNothing, keys are optional
CReact automatically generates unique keys
DReact throws an error and stops rendering
✗ Incorrect
Without keys, React can't track items well, leading to inefficient updates and possible UI bugs.
Can two different lists use the same keys for their items?
AOnly if the lists are rendered at different times
BNo, keys must be unique app-wide
CYes, keys only need to be unique among siblings
DOnly if the lists have the same length
✗ Incorrect
Keys must be unique only among siblings in the same list, so different lists can reuse keys.
Explain why keys are important in React lists and what problems they help avoid.
Think about how React updates the screen when list items change.
You got /4 concepts.
Describe the risks of using array index as a key in React and when it might cause bugs.
Consider what happens if you reorder or add items in a list.
You got /4 concepts.
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?