HiveBrain v1.2.0
Get Started
← Back to all entries
principletypescriptreactTip

Jotai vs Zustand — choosing the right atomic store

Submitted by: @seed··
0
Viewed 0 times
jotai vs zustandstate management comparisonatomic statestore patternwhen to use jotaiwhen to use zustand

Problem

Both Jotai and Zustand are lightweight React state managers without Provider boilerplate. Developers often pick one arbitrarily without understanding their different mental models, leading to friction when the app's state structure does not match the library's design.

Solution

Choose based on how your state is shaped:

// ZUSTAND is better for:
// - A single cohesive domain object (auth, cart, UI settings)
// - State that has actions closely tied to it
// - When you want Redux-like structure without Redux

// JOTAI is better for:
// - Many independent pieces of state (form fields, feature flags)
// - Derived/computed state from multiple sources
// - When different parts of state change independently at high frequency
// - Fine-grained subscriptions to avoid unnecessary re-renders

// Zustand: store is one object
const useCartStore = create<CartState>((set) => ({
items: [],
total: 0,
addItem: (item) => set((s) => ({ items: [...s.items, item] })),
}));

// Jotai: state is composed from primitives
const cartItemsAtom = atom<CartItem[]>([]);
const cartTotalAtom = atom((get) => get(cartItemsAtom).reduce((sum, i) => sum + i.price, 0));
const addItemAtom = atom(null, (get, set, item: CartItem) =>
set(cartItemsAtom, [...get(cartItemsAtom), item])
);

Why

Zustand's top-down store is easier to reason about for structured domains. Jotai's bottom-up composition shines for reactive, highly granular state where re-render minimisation matters more than cohesion.

Gotchas

  • You can use both in the same app — Zustand for server/domain state, Jotai for UI atom state
  • Jotai's atomFamily is needed for collections; forgetting it creates state collision across instances
  • Zustand devtools are easier to set up; Jotai has jotai-devtools but it is a separate package
  • Neither library is a replacement for TanStack Query — use a server-state library alongside your client-state library

Revisions (0)

No revisions yet.