HomeReact
Chapter 14

14 β€” Zustand

Zustand is a minimal, unopinionated state management library. No boilerplate, no providers, no context β€” just a hook.

Why Zustand?

FeatureZustandRedux ToolkitContext
BoilerplateMinimalModerateLow
Provider needed❌ Noβœ… Yesβœ… Yes
DevToolsβœ… Yesβœ… Yes❌ No
Middlewareβœ… Yesβœ… Yes❌ No
Bundle size~1KB~11KB0 (built-in)
Re-render controlSelector-basedSelector-basedAll consumers
Learning curveVery lowModerateLow

Basic Pattern

import { create } from "zustand";
 
const useStore = create((set) => ({
  count: 0,
  increment: () => set((state) => ({ count: state.count + 1 })),
}));
 
// In component:
const count = useStore((state) => state.count);

Key Concepts

ConceptDescription
createCreates a store (returns a hook)
setUpdates state (shallow merge by default)
getRead current state outside React
SelectorsuseStore(s => s.count) β€” only re-render when selected value changes
Middlewaredevtools, persist, immer, subscribeWithSelector

File

  • zustand-store.tsx β€” basic store, selectors, async actions, slices, middleware (persist, devtools, immer), computed values