08

Frontend Architecture

State taxonomy, colocation, scaling

GoalLearn to ask 'what kind of state is this?' before reaching for a tool. Master the state taxonomy, colocation, Context performance, and how large codebases are structured.

Lessons

The state taxonomy

Every architectural mistake comes from putting state in the wrong home:

KindHomeTool
Server state (API data)Server cacheReact Query / SWR
Global client stateGlobal storeContext / Zustand / Redux
Local UI stateComponentuseState
URL state (filters, page)URLRouter params
Form stateFormReact Hook Form
"Should I use Redux?" → "What kind of state is it?" Most of the time it's server state → React Query, not Redux.

Colocation rule

State should live as close to where it's used as possible.

Lift state only when siblings genuinely share it — and only to the nearest common ancestor.

❌ Over-lifted: App holds dropdownOpen (only Dropdown uses it)
✅ Colocated: Dropdown holds its own dropdownOpen

Colocated state re-renders fewer components, is easier to understand, and is deleted automatically when the component is deleted.

Context performance trap

Every consumer of a Context re-renders when the Context value changes — even consumers that use only part of the value.

jsx
// ❌ One big context — changing cart re-renders theme consumers too
const AppContext = createContext();
<AppContext.Provider value={{ user, theme, cart }}>

// The value object is NEW every render → new reference → all consumers re-render

Fixes: 1. Split contexts: UserContext, ThemeContext, CartContext 2. Memoize the value: useMemo(() => ({ user, theme }), [user, theme]) 3. For high-frequency: use Zustand (slice-level subscriptions)

Challenges1

Write working code. Run to see output. Check to grade.

CH1
Classify the state
Warm-up

For each piece of state, log the correct home ("react-query", "usestate", "url", "context-zustand", or "rhf"):

(a) Products fetched from /api/products (b) Mobile nav open/closed (c) Search filter that should survive page refresh (d) Theme preference used everywhere (e) Values in a signup form

JavaScript

References