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
Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9
lines 1–120why, one step back1---2title: Slices Pattern3nav: 64---56## Slicing the store into smaller stores78Your store can become bigger and bigger and tougher to maintain as you add more features.910You can divide your main store into smaller individual stores to achieve modularity. This is simple to accomplish in Zustand!1112The first individual store:1314```js15export const createFishSlice = (set) => ({16 fishes: 0,17 addFish: () => set((state) => ({ fishes: state.fishes + 1 })),18})19```2021Another individual store:2223```js24export const createBearSlice = (set) => ({25 bears: 0,26 addBear: () => set((state) => ({ bears: state.bears + 1 })),27 eatFish: () => set((state) => ({ fishes: state.fishes - 1 })),28})29```3031You can now combine both the stores into **one bounded store**:3233```js34import { create } from 'zustand'35import { createBearSlice } from './bearSlice'36import { createFishSlice } from './fishSlice'3738export const useBoundStore = create((...a) => ({39 ...createBearSlice(...a),40 ...createFishSlice(...a),41}))42```4344### Usage in a React component4546```jsx47import { useBoundStore } from './stores/useBoundStore'4849function App() {50 const bears = useBoundStore((state) => state.bears)51 const fishes = useBoundStore((state) => state.fishes)52 const addBear = useBoundStore((state) => state.addBear)53 return (54 <div>55 <h2>Number of bears: {bears}</h2>56 <h2>Number of fishes: {fishes}</h2>57 <button onClick={() => addBear()}>Add a bear</button>58 </div>59 )60}6162export default App63```6465### Updating multiple stores6667You can update multiple stores, at the same time, in a single function.6869```js70export const createBearFishSlice = (set, get) => ({71 addBearAndFish: () => {72 get().addBear()73 get().addFish()74 },75})76```7778Combining all the stores together is the same as before.7980```js81import { create } from 'zustand'82import { createBearSlice } from './bearSlice'83import { createFishSlice } from './fishSlice'84import { createBearFishSlice } from './createBearFishSlice'8586export const useBoundStore = create((...a) => ({87 ...createBearSlice(...a),88 ...createFishSlice(...a),89 ...createBearFishSlice(...a),90}))91```9293## Adding middlewares9495Adding middlewares to a combined store is the same as with other normal stores.9697Adding [`persist` middleware](../../reference/integrations/persisting-store-data.md) to our `useBoundStore`:9899```js100import { create } from 'zustand'101import { createBearSlice } from './bearSlice'102import { createFishSlice } from './fishSlice'103import { persist } from 'zustand/middleware'104105export const useBoundStore = create(106 persist(107 (...a) => ({108 ...createBearSlice(...a),109 ...createFishSlice(...a),110 }),111 { name: 'bound-store' },112 ),113)114```115116Please keep in mind you should only apply middlewares in the combined store. Applying them inside individual slices can lead to unexpected issues.117118## Usage with TypeScript119120A detailed guide on how to use the slice pattern in Zustand with TypeScript can be found [here](./advanced-typescript.md#slices-pattern).