slices-pattern.md
This file
Path
docs/learn/guides/slices-pattern.md
120 lines · 2.9 KiB · Markdown
Last changed by
Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121---title: Slices Patternnav: 6---
## Slicing the store into smaller stores
Your store can become bigger and bigger and tougher to maintain as you add more features.
You can divide your main store into smaller individual stores to achieve modularity. This is simple to accomplish in Zustand!
The first individual store:
```jsexport const createFishSlice = (set) => ({ fishes: 0, addFish: () => set((state) => ({ fishes: state.fishes + 1 })),})```
Another individual store:
```jsexport const createBearSlice = (set) => ({ bears: 0, addBear: () => set((state) => ({ bears: state.bears + 1 })), eatFish: () => set((state) => ({ fishes: state.fishes - 1 })),})```
You can now combine both the stores into **one bounded store**:
```jsimport { create } from 'zustand'import { createBearSlice } from './bearSlice'import { createFishSlice } from './fishSlice'
export const useBoundStore = create((...a) => ({ ...createBearSlice(...a), ...createFishSlice(...a),}))```
### Usage in a React component
```jsximport { useBoundStore } from './stores/useBoundStore'
function App() { const bears = useBoundStore((state) => state.bears) const fishes = useBoundStore((state) => state.fishes) const addBear = useBoundStore((state) => state.addBear) return ( <div> <h2>Number of bears: {bears}</h2> <h2>Number of fishes: {fishes}</h2> <button onClick={() => addBear()}>Add a bear</button> </div> )}
export default App```
### Updating multiple stores
You can update multiple stores, at the same time, in a single function.
```jsexport const createBearFishSlice = (set, get) => ({ addBearAndFish: () => { get().addBear() get().addFish() },})```
Combining all the stores together is the same as before.
```jsimport { create } from 'zustand'import { createBearSlice } from './bearSlice'import { createFishSlice } from './fishSlice'import { createBearFishSlice } from './createBearFishSlice'
export const useBoundStore = create((...a) => ({ ...createBearSlice(...a), ...createFishSlice(...a), ...createBearFishSlice(...a),}))```
## Adding middlewares
Adding middlewares to a combined store is the same as with other normal stores.
Adding [`persist` middleware](../../reference/integrations/persisting-store-data.md) to our `useBoundStore`:
```jsimport { create } from 'zustand'import { createBearSlice } from './bearSlice'import { createFishSlice } from './fishSlice'import { persist } from 'zustand/middleware'
export const useBoundStore = create( persist( (...a) => ({ ...createBearSlice(...a), ...createFishSlice(...a), }), { name: 'bound-store' }, ),)```
Please keep in mind you should only apply middlewares in the combined store. Applying them inside individual slices can lead to unexpected issues.
## Usage with TypeScript
A detailed guide on how to use the slice pattern in Zustand with TypeScript can be found [here](./advanced-typescript.md#slices-pattern).