Module 2 of 7

Hooks

A component function runs from the top on every render, so it can't remember anything by itself. Hooks are how it asks React to remember things (useState, useReducer, useRef), to do things after rendering (useEffect, useLayoutEffect) and to stay stable between renders (useMemo, useCallback, useId). This module builds them up on the product store: a search box, a back button, a cart badge, favorites, a zoom effect, review forms, a cart with undo, and a cart page that re-renders more than it should.

Every log, order of calls and "before/after" in these notes was recorded by running the product store in Chrome (examples/product-store/src/lessons/hooks).

Two facts explain most hook behavior: each render is a separate function call with its own values (closures), and React compares things with Object.is (dependencies, memo props, state). Keep both in mind while reading.

Start with “Managing UI State”All notesInterview questions

The notes

Read them in order. Each one builds on the ones before it.

01Must know

Managing UI State

useState, controlled inputs, deriving values instead of storing them twice, and lazy initial state.

Read
02Must know

Side Effects

useEffect for syncing with the outside world, and the cleanup that stops listeners from piling up.

Read
03Must know

React Lifecycle

Mount, update, unmount: the recorded order of renders, layout effects, effects and cleanups.

Read
04Must know

Lifting State

Move state up so siblings can share it, and back down (colocate) when only one component needs it.

Read
06Good to know

The useId Hook

Unique, SSR-safe ids for label/input pairs in components that render many times.

Read
07Must know

Building a Cart

Updater functions, immutable updates, saving to localStorage and an undo history, in one cart.

Read
08Must know

useState vs useReducer

Independent values → useState; values that change together → useReducer. The stale-closure bug that decides it.

Read
09Must know

React Re-rendering

What triggers a re-render, why memo fails on new objects, stale closures, and when to use useMemo/useCallback.

Read