# Creating a Zustand Store

> Ditch the boilerplate. Create a global hook in 5 lines of code.

Subject: State Management | Lesson 4 of 22 | 4 min read
Interactive lesson: https://codewithanimation.in/state-management/creating-zustand-store
Previous: [Flux & Unidirectional Data Flow](https://codewithanimation.in/state-management/flux-architecture.md) | Next: [Actions & State Mutations](https://codewithanimation.in/state-management/zustand-actions.md)

## What is Zustand?

Zustand (German for "state") is a fast, minimal state management library for React. While Redux requires actions, reducers, and massive boilerplate, Zustand lets you create a global store that behaves exactly like a custom React hook.

You create a store using the `create` function. It holds your state (data) and your actions (functions that modify the data) in one simple object.

## Why ditch Context for Zustand?

1. **No Provider Hell**: Zustand stores exist *outside* the React component tree. You don't need to wrap your `<App />` in a `<ZustandProvider>`. Any component can simply import the hook and use it.

2. **Granular Re-renders**: Unlike React Context, which forces every consumer to re-render when any property changes, Zustand allows components to "select" only the specific data they care about, eliminating unnecessary re-renders.

3. **Less Boilerplate**: No need to write `createContext`, `useContext`, or a custom Provider component just to share a single variable.

## How to use `create`

You define a store by calling `create`. It takes a callback function that receives a `set` function (used to update state) and a `get` function (used to read current state).

The result is a custom React hook (conventionally named starting with `use`, like `useAuthStore` or `useCartStore`).

```tsx
import { create } from 'zustand';

export const useBearStore = create((set) => ({
  bears: 0,
  increase: () => set((state) => ({ bears: state.bears + 1 })),
}));
```

## Common mistake: The Context Refactor Pain

If you've ever built an app using `useState` and drilled props down 4 levels, you eventually realize you need global state. With Context, you have to create a new file, write a Provider, wrap your component tree, and then update all your children to use `useContext`. With Zustand, you just extract your state into `create()` and import the hook directly where it's needed. The refactor takes seconds.

## Tip: Keep Stores Focused

You can create as many Zustand stores as you want. Instead of building one massive `useGlobalStore`, it is a best practice to create multiple, focused stores (e.g., `useUserStore`, `useCartStore`, `useThemeStore`).

## Try it: Boilerplate Comparison

Compare the amount of code required to create a global counter using React Context vs Zustand. Notice how Zustand collapses the Context, Provider, and Hook into a single clean declaration.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/state-management/creating-zustand-store

## Check yourself

### Question 1

What React component must you use to wrap your application so a Zustand store can be accessed by child components?

- A. <ZustandProvider store={store}>
- B. <StoreContext.Provider>
- C. None. Zustand stores do not require a Provider wrapper.

Correct answer: C

Zustand stores are external to the React component tree. You simply import the generated hook into any component that needs it, with absolutely zero Provider wrappers required.

- Why not A: Zustand doesn't use Context Providers. Stores exist outside the React tree.
- Why not B: That's how React Context works, not Zustand.

## Remember this

- Zustand creates global hooks using the `create` function.
- No Provider wrappers are required in your component tree.
- State and actions are defined together in a single object.
- Zustand solves the Context re-render trap out of the box.
