JavaScript Curriculum
Lists & Keys
easyThe coffee shop menu has dozens of items. Instead of writing a MenuCard for each one, you map over the items array and render one component per item. When the barista adds a special, the list updates with one state change.
Lists & Keys
The .map() method is React's primary tool for rendering lists. The key prop is not optional — it is how React tracks which items changed across renders, enabling efficient updates and preventing subtle bugs.
Four List Rendering Patterns
From basic to filter+map and Fragment lists:
const items = [ { id: 1, name: 'Espresso', price: 2.50 }, { id: 2, name: 'Cappuccino', price: 3.50 }, ] function Menu() { return ( <div className="menu"> {items.map(item => ( <MenuCard key={item.id} name={item.name} price={item.price} /> ))} </div> ) }
Why Keys Matter — Stable vs Unstable
See the difference between key={item.id} and key={index}:
key="a"key="b"key="c"// Reorder or add/remove items to see key behavior
React matches nodes by identity. Reorder moves the DOM node. Remove deletes only that node.
React matches by position. Reorder destroys and recreates. Can cause bugs with state, animations, and focus.
Dynamic List — Full CRUD
Add, toggle, delete, filter — key={id} throughout:
key=1key=2key=3Your Challenge
Build a menu with filter buttons (All / Coffee / Food / Tea). State: items array and category string. Derive: visible = items.filter(i => category === 'all' || i.category === category). Render: visible.map(item => <MenuCard key={item.id} {...item} />). Add a count: {visible.length} items.
Challenge
Build a filterable menu list: items array in state, filter state ('all'/'coffee'/'food'). Derive visible = items.filter(...).map(...). Add, remove, and reorder items. Use item.id as key — never the array index.
Lists & Keys
easyThe coffee shop menu has dozens of items. Instead of writing a MenuCard for each one, you map over the items array and render one component per item. When the barista adds a special, the list updates with one state change.
Lists & Keys
The .map() method is React's primary tool for rendering lists. The key prop is not optional — it is how React tracks which items changed across renders, enabling efficient updates and preventing subtle bugs.
Four List Rendering Patterns
From basic to filter+map and Fragment lists:
const items = [ { id: 1, name: 'Espresso', price: 2.50 }, { id: 2, name: 'Cappuccino', price: 3.50 }, ] function Menu() { return ( <div className="menu"> {items.map(item => ( <MenuCard key={item.id} name={item.name} price={item.price} /> ))} </div> ) }
Why Keys Matter — Stable vs Unstable
See the difference between key={item.id} and key={index}:
key="a"key="b"key="c"// Reorder or add/remove items to see key behavior
React matches nodes by identity. Reorder moves the DOM node. Remove deletes only that node.
React matches by position. Reorder destroys and recreates. Can cause bugs with state, animations, and focus.
Dynamic List — Full CRUD
Add, toggle, delete, filter — key={id} throughout:
key=1key=2key=3Your Challenge
Build a menu with filter buttons (All / Coffee / Food / Tea). State: items array and category string. Derive: visible = items.filter(i => category === 'all' || i.category === category). Render: visible.map(item => <MenuCard key={item.id} {...item} />). Add a count: {visible.length} items.
Challenge
Build a filterable menu list: items array in state, filter state ('all'/'coffee'/'food'). Derive visible = items.filter(...).map(...). Add, remove, and reorder items. Use item.id as key — never the array index.