0
0
Reactframework~10 mins

Common list rendering mistakes in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Common list rendering mistakes
Start: Render list component
Map over array
Return JSX for each item
Check for unique key prop
Render list
React updates DOM efficiently
This flow shows how React renders a list by mapping over data and the importance of unique keys to help React update the UI correctly.
Execution Sample
React
const items = ['apple', 'banana', 'cherry'];
return (
  <ul>
    {items.map(item => <li key={item}>{item}</li>)}
  </ul>
);
This code renders a list of fruits with the key prop on list items.
Execution Table
StepActionJSX OutputKey Prop Present?React Behavior
1Start rendering list<ul>...</ul>N/ABegin mapping over items
2Map item 'apple'<li>apple</li>NoWarning: Missing key, React may re-use elements incorrectly
3Map item 'banana'<li>banana</li>NoWarning: Missing key, React may re-use elements incorrectly
4Map item 'cherry'<li>cherry</li>NoWarning: Missing key, React may re-use elements incorrectly
5Finish rendering<ul><li>apple</li><li>banana</li><li>cherry</li></ul>NoReact renders list but warns about keys
6Fix: Add key={item}<li key='apple'>apple</li> ...YesReact uses keys to track items efficiently
7Render with keys<ul><li key='apple'>apple</li><li key='banana'>banana</li><li key='cherry'>cherry</li></ul>YesReact updates DOM correctly without warnings
8Mistake: Use index as key<li key='0'>apple</li> ...YesWorks but can cause bugs if list changes order
9Finish with index keys<ul><li key='0'>apple</li><li key='1'>banana</li><li key='2'>cherry</li></ul>YesReact may mis-update items on reorder
10ExitN/AN/ARendering complete
💡 Rendering stops after all list items are processed and React updates the DOM.
Variable Tracker
VariableStartAfter 1After 2After 3Final
items['apple', 'banana', 'cherry']SameSameSameSame
key propundefinedundefinedundefinedundefineddefined or index depending on fix
Key Moments - 3 Insights
Why does React warn about missing keys in list items?
React uses keys to identify which items changed, added, or removed. Without keys (see execution_table rows 2-5), React cannot track items properly and may re-use DOM elements incorrectly.
Is using the array index as a key always safe?
Using index as key (see execution_table rows 8-9) works if the list never changes order or items. But if items reorder or change, React may update wrong elements, causing bugs.
What happens if keys are not unique?
If keys are duplicated, React cannot distinguish items properly, leading to unexpected UI updates or bugs. Keys must be unique among siblings.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3. What is the key prop value for the 'banana' item?
AIndex 1
BNo key prop present
C'banana'
D'apple'
💡 Hint
Check the 'Key Prop Present?' column at step 3 in the execution_table.
At which step does React start using keys to track list items correctly?
AStep 6
BStep 5
CStep 8
DStep 2
💡 Hint
Look for the first step where 'Key Prop Present?' is 'Yes' and React behavior improves.
If you use index as key (step 8), what problem might occur when the list changes?
AReact will throw an error
BReact will ignore the list
CReact may update wrong items causing UI bugs
DReact will duplicate items
💡 Hint
Refer to the React behavior described at step 9 in the execution_table.
Concept Snapshot
React list rendering requires a unique key prop on each item.
Keys help React track items between renders.
Missing keys cause warnings and inefficient updates.
Using array index as key can cause bugs if list order changes.
Always use stable, unique keys like IDs or unique strings.
Full Transcript
This lesson shows how React renders lists by mapping over arrays and returning JSX for each item. React requires a unique key prop on each list item to track changes efficiently. Without keys, React warns and may re-use DOM elements incorrectly. Using array index as key works only if the list never changes order. Unique, stable keys prevent UI bugs and improve performance.