Skip to main content

Zustand fundamentals

This handbook targets Zustand 5.0.14.

Zustand is a lightweight external store with a hook-oriented React API.

Mental modelโ€‹

Zustand external-store model
Zustand store

State + actions + getState + setState + subscribe

CartBadge
Reads only the selected cart result.
PreferencesPanel
Reads only the selected preferences result.

The store exists outside React. Components subscribe to selected results from that store.

Installโ€‹

npm install zustand

Create a storeโ€‹

import { create } from 'zustand'

type CounterStore = {
count: number
increment: () => void
decrement: () => void
}

export const useCounterStore = create<CounterStore>()((set) => ({
count: 0,
increment: () => set((state) => ({ count: state.count + 1 })),
decrement: () => set((state) => ({ count: state.count - 1 })),
}))

create returns a React Hook with store utilities attached.

Select stateโ€‹

function CounterValue() {
const count = useCounterStore((state) => state.count)
return <output>{count}</output>
}
Selector subscription flow
Whole store
Selected result
Component

Prefer selecting the smallest state a component needs.

Select actionsโ€‹

function CounterControls() {
const increment = useCounterStore((state) => state.increment)
const decrement = useCounterStore((state) => state.decrement)

return (
<div>
<button onClick={decrement}>-</button>
<button onClick={increment}>+</button>
</div>
)
}

Actions colocated with state create a clear feature API.

Update from previous stateโ€‹

When the next value depends on the current value, use an updater:

increment: () => set((state) => ({ count: state.count + 1 }))

For object-shaped stores, set shallow-merges by default.

Treat arrays and objects immutablyโ€‹

addItem: (item) =>
set((state) => ({
items: [...state.items, item],
}))

Avoid in-place mutation of nested objects and arrays when selector identity matters.

Derived valuesโ€‹

Do not duplicate values that can be derived.

const itemCount = useCartStore((state) => state.items.length)

or:

const selectCartTotal = (state: CartStore) =>
state.items.reduce(
(total, item) => total + item.price * item.quantity,
0,
)

Access outside Reactโ€‹

const count = useCounterStore.getState().count
const unsubscribe = useCounterStore.subscribe((state) => {
console.log(state.count)
})

This external access is one reason Zustand can fit state that must be shared with non-React code.

Focused storesโ€‹

Do not default to one giant global store.

Multiple focused stores preserve domain ownership
cartStore
Cart-specific state and actions.
editorStore
Editor interaction state.
preferencesStore
User interface preferences.

Multiple stores can preserve domain ownership.

Zustand vs Contextโ€‹

Context vs Zustand
Context

Value follows provider tree ยท provider scope is explicit ยท consumers read the Context value.

Zustand

External store lifetime ยท selector subscriptions ยท usable from non-React code.

Use Context when provider scope naturally expresses ownership. Consider Zustand when independent selector subscriptions or external access matter.

Zustand vs Redux Toolkitโ€‹

Redux Toolkit is more structured around actions, slices, reducers, middleware, and a central event flow.

Zustand is lower ceremony and store-centric.

Choose based on architecture requirements, not popularity.

Common mistakesโ€‹

  • selecting the whole store in every component;
  • putting server cache data in Zustand without a cache lifecycle strategy;
  • creating one universal store for unrelated domains;
  • bypassing named domain actions with raw updates everywhere.

Testingโ€‹

The store can be tested directly. Reset shared store state between tests so one test does not influence another.

Interview questionsโ€‹

Junior: What does create return?

Mid-level: Why are selectors important?

Senior: When would you choose Zustand over Context or Redux Toolkit?

Staff: How would you prevent module-level stores from becoming hidden global coupling in a large application?

Referencesโ€‹