Zust2help May 2026
// Option 1: getState() const handleClick = () => const currentCount = useStore.getState().count console.log(currentCount)
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. zust2help
Problem 1: Component Re-renders Too Often Issue: Using the entire store causes re-renders when any state changes. // Option 1: getState() const handleClick = ()
// Option 2: Use useRef with store subscription Solution: Define your store's type. // Example: Only persist on client side const
// 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.