Why should you always provide a key prop when rendering lists in React?
The key helps React identify which items changed, were added, or removed. This improves performance and prevents bugs during re-rendering.
Click to reveal answer
intermediate
What happens if you use the array index as the key in a list?
Using the index as key can cause problems when the list changes order or items are added/removed. React may reuse components incorrectly, causing UI bugs.
Click to reveal answer
beginner
What is a common mistake when rendering lists without unique keys?
React will warn about missing keys, and without unique keys, it can't track items properly. This can lead to unexpected UI updates or incorrect component states.
Click to reveal answer
intermediate
Why should list items be stable and not change their key between renders?
If keys change, React treats items as new and recreates them, losing any local state or causing unnecessary re-renders.
Click to reveal answer
beginner
What mistake can happen if you forget to return the list item inside a map()?
If you forget to return the JSX inside map(), the list will render empty or undefined items, causing no visible output or errors.
Click to reveal answer
What is the main purpose of the key prop in React list rendering?
ATo add event listeners to list items
BTo uniquely identify list items for efficient updates
CTo style the list items differently
DTo sort the list items automatically
✗ Incorrect
The key helps React track items between renders for better performance and correct UI updates.
Which of these is a bad choice for a key in a dynamic list?
AA UUID generated once per item
BA stable string unique to each item
CA unique ID from data
DThe array index
✗ Incorrect
Using the array index as key can cause bugs when the list changes order or items are added/removed.
What happens if you render a list without any key props?
AReact warns and may update inefficiently
BReact throws a runtime error
CThe list items get styled differently
DNothing happens, it works perfectly
✗ Incorrect
React warns about missing keys and may have inefficient or buggy updates without keys.
Why is it important that keys remain stable between renders?
ATo add animations to list items
BTo change the order of items automatically
CTo keep component state and avoid unnecessary re-renders
DTo prevent React from rendering the list
✗ Incorrect
Stable keys help React keep track of components and preserve their state.
What mistake causes a list to render nothing when using map()?
ANot returning the JSX element inside <code>map()</code>
BUsing unique keys
CUsing <code>filter()</code> instead of <code>map()</code>
DAdding keys to list items
✗ Incorrect
If you forget to return the JSX inside map(), the list renders empty.
Explain why keys are important in React list rendering and what can go wrong if they are missing or unstable.
Think about how React tracks items between renders.
You got /4 concepts.
Describe a common mistake when using map() to render lists and how to fix it.
Check your arrow function syntax inside <code>map()</code>.
You got /3 concepts.
Practice
(1/5)
1. Why is it important to add a unique key prop when rendering lists in React?
easy
A. It automatically sorts the list alphabetically.
B. It makes the list items look prettier on the screen.
C. It prevents the list from rendering at all.
D. It helps React identify which items have changed, added, or removed.
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 D
Quick Check:
Keys improve UI updates = A [OK]
Hint: Keys uniquely identify list items for React updates [OK]
Common Mistakes:
Thinking keys affect styling
Believing keys prevent rendering
Assuming keys sort the list
2. Which of the following is the correct way to add keys when rendering a list of items stored in items array?
easy
A. {items.map((item, index) => <li key={index}>{item}</li>)}
B. {items.map(item => <li key={item.id}>{item.name}</li>)}
C. {items.map(item => <li>{item.name}</li>)}
D. {items.map((item, i) => <li key={i + 1}>{item.name}</li>)}
Solution
Step 1: Check key uniqueness and stability
Using item.id ensures 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 B
Quick Check:
Use unique stable keys = B [OK]
Hint: Use unique IDs, not indexes, as keys [OK]
Common Mistakes:
Using array index as key
Omitting the key prop
Using non-unique keys
3. What will be the output of this React code snippet?