# The Slices Pattern

> Decompose a massive store into clean, domain-driven slice functions.

Subject: State Management | Lesson 7 of 22 | 5 min read
Interactive lesson: https://codewithanimation.in/state-management/zustand-slices-pattern
Previous: [Selectors & useShallow](https://codewithanimation.in/state-management/zustand-selectors-and-shallow.md) | Next: [Transient Updates](https://codewithanimation.in/state-management/zustand-transient-updates.md)

## What is the Slices Pattern?

In Zustand, the easiest way to organize state is simply to create multiple stores (e.g., `useUserStore` and `useCartStore`). However, if your state domains need to interact frequently, you might want a single combined store.

The 'Slices Pattern' is Zustand's way of splitting a single, monolithic store into multiple smaller, domain-specific functions called 'slices'. You then merge these slices together when calling `create()`.

## Why use slices?

If you cram authentication, user preferences, shopping cart data, and UI state into one massive `create()` call, your store file becomes thousands of lines long and impossible to read.

Slices let you move the cart logic to `cartSlice.ts` and the auth logic to `authSlice.ts`, keeping your codebase modular while still sharing a single global store.

## How to implement a slice

A slice is just a function that takes the store's `set` and `get` functions and returns an object containing that domain's state and actions.

```tsx
export const createBearSlice = (set) => ({
  bears: 0,
  addBear: () => set((state) => ({ bears: state.bears + 1 })),
});
```

Then, in your main store file, you merge the slices by spreading them into the `create` callback, passing the arguments (`set, get, store`) through:

```tsx
export const useBoundStore = create((...a) => ({
  ...createBearSlice(...a),
  ...createFishSlice(...a),
}));
```

## Common mistake: Typing Slices in TypeScript

The primary pain point with the slices pattern is TypeScript. Because the slice function needs to know about the entire store's type to satisfy the `set` function, typing slices can involve boilerplate. Zustand exports a `StateCreator` type specifically to type slices correctly: `StateCreator<SharedState, [], [], BearSlice>`.

## Tip: Multi-Store vs Single-Store Slices

Before reaching for the Slices pattern, ask yourself: 'Do these domains actually need to share state?' If the Auth store never needs to read from the Cart store, it's much simpler to just create two separate hooks (`useAuthStore` and `useCartStore`) rather than creating slices.

## Try it: Slice Architecture

Observe how domain-specific logic is isolated into independent slice functions, which are then combined to form the bound store hook.

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

## Check yourself

### Question 1

How do you combine multiple slices into a single Zustand store?

- A. Use the combineReducers function from Zustand.
- B. Spread the execution of each slice function inside the create callback, passing the arguments through.
- C. You can't. Zustand enforces one single file for the store.

Correct answer: B

You combine slices by spreading their return values into the main store: `...createBearSlice(...args), ...createFishSlice(...args)`.

- Why not A: Zustand doesn't use Redux-style reducers by default.
- Why not C: Zustand specifically documents the slices pattern to solve this.

## Remember this

- The Slices pattern splits a large store into smaller, domain-driven functions.
- Slices are merged together by spreading them inside the `create` callback.
- If state domains don't need to interact, creating separate stores is simpler.
- Use `StateCreator` in TypeScript to properly type your slice functions.
