What problem does it solve?
Zustand is a lightweight, TypeScript-first state management library for React in Next.js projects. It enables minimal boilerplate to manage global and shared state without reducers or context, helping teams build scalable UI patterns.
Core Features & Use Cases
- Type-safe store creation with explicit TypeScript types and the double-call pattern.
- Guidance for using Zustand in Client Components within Next.js while avoiding Server Components pitfalls.
- Middleware and patterns like devtools, persist, and slices to compose scalable stores and robust selectors.
- Real-world use cases across dashboards, feature flags, user sessions, and cross-page state sharing.
Quick Start
To begin, install zustand, define a simple store with a Bear-like state, and use it inside a client component.
- Install: npm install zustand
- Create a store: define a BearState type and a store using create<BearState>()((set) => ({ bears: 0, increase: (by) => set((s) => ({ bears: s.bears + by })) }))
- Use in a client component:
- Add "use client" at the top of the component file
- Import and consume the store: const bears = useBearStore((state) => state.bears)