JavaScript Curriculum
State — useState
easyThe coffee shop cart needs to update when customers add items. The order form needs to track what the user is typing. The filter needs to remember which category is active. All of this is state.
State — useState
State is memory that persists across re-renders. Every time you call the setter, React schedules a re-render and your component function runs again with the new value. This is the core mechanism that makes React UIs dynamic.
useState — Interactive Explorer
Try all three common state types — number, string, boolean:
const [count, setCount] = useState(0) // Functional update (safe): setCount(c => c + 1) // → 1 setCount(c => c - 1) // → -1 setCount(0) // reset
What Triggers a Re-render?
Not all state updates cause a re-render — learn the rules:
State Update Patterns
The rules for updating primitives, objects, and arrays correctly:
Use the functional form when the new state depends on the old state. Avoids stale closures.
// ⚠ Can be stale in async contexts: const [count, setCount] = useState(0) // If called multiple times rapidly, // count may be stale: setCount(count + 1) setCount(count + 1) // both use same stale count! // result: count = 1, not 2
// ✓ Functional update — always fresh: setCount(prev => prev + 1) setCount(prev => prev + 1) // result: count = 2 ✓ // Toggle pattern: setIsOpen(prev => !prev) // Append to array: setItems(prev => [...prev, newItem])
Your Challenge
Build CartSummary: state is items = []. addItem(item) → setItems(prev => [...prev, item]). removeItem(id) → setItems(prev => prev.filter(x => x.id !== id)). Display total: items.reduce((sum, x) => sum + x.price, 0).toFixed(2). Add 3 buttons to add items and verify the total updates.
Challenge
Build a CartSummary component with useState. Track an items array — start empty. Add an addItem(item) function that appends to the array immutably. Add a removeItem(id) function that filters. Display the item count and total price.
State — useState
easyThe coffee shop cart needs to update when customers add items. The order form needs to track what the user is typing. The filter needs to remember which category is active. All of this is state.
State — useState
State is memory that persists across re-renders. Every time you call the setter, React schedules a re-render and your component function runs again with the new value. This is the core mechanism that makes React UIs dynamic.
useState — Interactive Explorer
Try all three common state types — number, string, boolean:
const [count, setCount] = useState(0) // Functional update (safe): setCount(c => c + 1) // → 1 setCount(c => c - 1) // → -1 setCount(0) // reset
What Triggers a Re-render?
Not all state updates cause a re-render — learn the rules:
State Update Patterns
The rules for updating primitives, objects, and arrays correctly:
Use the functional form when the new state depends on the old state. Avoids stale closures.
// ⚠ Can be stale in async contexts: const [count, setCount] = useState(0) // If called multiple times rapidly, // count may be stale: setCount(count + 1) setCount(count + 1) // both use same stale count! // result: count = 1, not 2
// ✓ Functional update — always fresh: setCount(prev => prev + 1) setCount(prev => prev + 1) // result: count = 2 ✓ // Toggle pattern: setIsOpen(prev => !prev) // Append to array: setItems(prev => [...prev, newItem])
Your Challenge
Build CartSummary: state is items = []. addItem(item) → setItems(prev => [...prev, item]). removeItem(id) → setItems(prev => prev.filter(x => x.id !== id)). Display total: items.reduce((sum, x) => sum + x.price, 0).toFixed(2). Add 3 buttons to add items and verify the total updates.
Challenge
Build a CartSummary component with useState. Track an items array — start empty. Add an addItem(item) function that appends to the array immutably. Add a removeItem(id) function that filters. Display the item count and total price.