// Example: Only persist on client side const useStore = create( persist( (set) => ( /* state */ ), name: 'my-store', getStorage: () => if (typeof window !== 'undefined') return localStorage return dummyStorage , ) ) 1. Splitting Stores Avoid one giant store. Split by domain.
Use persist with a skipHydration option or conditionally access storage.
// Update state useStore.setState( count: 100 )
// reducer, actions, constants, etc. const mapState = (state) => ( count: state.counter.count ) const mapDispatch = increment, decrement
const useBearStore = create<BearState>((set) => ( bears: 0, addBear: () => set((state) => ( bears: state.bears + 1 )), eatFish: () => set((state) => ( fishes: state.fishes - 1 )), )) Solution: Use the persist middleware.
Problem 1: Component Re-renders Too Often Issue: Using the entire store causes re-renders when any state changes.
// store/userStore.js export const useUserStore = create((set) => ( user: null, setUser: ... )) // store/cartStore.js export const useCartStore = create((set) => ( items: [], addItem: ... )) Zustand supports Redux DevTools, persistence, and custom middleware.