Creating Your First Zustand Store
You'll learn: How to use Zustand's create() function to define a store containing both state and actions in one simple object.
The Core Idea
Zustand's create() function is where you define your entire store. Unlike Context or Redux, there's no provider setup, no action types, no dispatch—just a single function that returns a hook. Inside create(), you pass a function that receives set (to update state) and returns an object containing your state values and action functions, all living together.
Think of it like defining a mini-database with built-in methods. You're not just storing data; you're also declaring how that data can change, all in one place.
The Pattern
import { create } from 'zustand';
const useStore = create((set) => ({
// Initial state
count: 0,
user: null,
// Actions that modify state
increment: () => set((state) => ({ count: state.count + 1 })),
decrement: () => set((state) => ({ count: state.count - 1 })),
setUser: (user) => set({ user }),
}));
The set function is how you update state. You can pass an object directly (set({ user })) or use a function to access current state (set((state) => ({ count: state.count + 1 }))). The returned hook (useStore) can be used in any component without a provider.
Why This Design Works
State and actions coexist naturally—no separation between "what" and "how." The store is just a hook you import and use. No wrapping your app, no context drilling, no ceremony.
Key Takeaway: Zustand's create() function defines your entire store as a single object containing both state values and action methods, returning a hook you can use anywhere in your app without providers.