โšกLearnIT
Engineering ยท Topic 11

๐Ÿ—‚๏ธState Management

11.1The landscape: Context vs Redux vs Recoil vs Zustand

Context APIRedux (Toolkit)RecoilZustand
Learning curveEasyModerate (much better w/ RTK)MediumEasy
BoilerplateMinimalModerateLightVery light
DevToolsNone built-inExcellent (time-travel)GoodGood (via middleware)
Re-render controlCoarse โ€” all consumers re-render on any value changeFine (selectors + useSelector)Fine (atom-level)Fine (selector subscriptions)
AsyncCustom hooksThunks / RTK Query / SagaAsync selectorsIn-actions / any async
Best forTheme, auth, locale โ€” low-frequency, app-wide valuesLarge apps, complex flows, time-travel debugging, big teamsAtom-based fine-grained reactivityMost apps wanting simple + performant global state

The Context gotcha (interviewers love this): Context isn't a state manager โ€” it's a dependency injection / propagation mechanism. Every component consuming a context re-renders whenever the context value changes, regardless of which part they use. A single monolithic context holding 20 fields means a change to any one field re-renders every consumer. Fixes: split into multiple focused contexts, or memoize the value, or use a real state library for high-frequency state. Keep Context for genuinely low-frequency, app-wide values (theme, current user, locale).

Rule of thumb progression: local useState โ†’ lift state up โ†’ Context for low-frequency shared values โ†’ Zustand (or Redux Toolkit) when you have high-frequency cross-component state or need fine-grained subscriptions.

11.2Why Zustand shines for frequent updates

import { create } from 'zustand';
import { subscribeWithSelector } from 'zustand/middleware';

const useStore = create(subscribeWithSelector((set) => ({
  tickets: [],
  filters: {},
  notifications: [],
  setTickets: (tickets) => set({ tickets }),
  updateTicket: (id, patch) => set((s) => ({
    tickets: s.tickets.map((t) => (t.id === id ? { ...t, ...patch } : t)),
  })),
})));

// Each component subscribes ONLY to the slice it uses.
// TicketList re-renders only when tickets change โ€” NOT when notifications arrive.
function TicketList() {
  const tickets = useStore((s) => s.tickets);       // selector = precise subscription
  const select  = useStore((s) => s.updateTicket);
  /* ... */
}

Why it fits high-frequency state: granular selector subscriptions (only subscribed slices re-render), no Provider wrapping the whole tree, batching within a tick, minimal boilerplate, and it works outside React too (useStore.getState() / useStore.setState()).

11.3Normalized state (recap โ€” it's a state-management pattern too)

Keep entities in id-keyed maps + index lists (see 3.6). Benefits: O(1) lookup and update, no deep re-derivation on a single change, no duplicated data to keep in sync. This is exactly what Redux's createEntityAdapter gives you, and what you'd hand-roll in Zustand.

11.4Decision guide

  • Large, frequently-updated lists โ†’ Zustand + virtual scroll + normalization
  • Real-time socket data โ†’ batching + useTransition
  • Cross-component state โ†’ Zustand with granular selectors (avoid one big Context)
  • Simple local state โ†’ useState, kept local
  • Derived/computed values โ†’ useMemo with correct deps (or a store selector)
  • Complex flows + big team + time-travel debugging โ†’ Redux Toolkit (+ RTK Query for server state)
  • Server state specifically โ†’ react-query/SWR, not Redux/Zustand. See 14.2 โ€” this is a distinction interviewers increasingly expect.

๐Ÿง  Quiz: State Management

2 questions

1Why does Zustand shine for very frequent state updates?

2Putting a rapidly-changing value in React Context typically causes:

Answered 0/2