Zustand Computed State is a lightweight, TypeScript-friendly middleware designed for the Zustand state management library. It allows developers to define derived or 'computed' state values that automatically update whenever their base dependencies in the Zustand store change. This eliminates the need for manual re-computation of derived values, simplifying state logic and improving performance by only re-calculating when necessary. The current stable version is 0.2.0, indicating it's in an early but active development phase, with releases likely occurring as features stabilize. Its key differentiator is its straightforward integration into Zustand's middleware chain, providing a clear pattern for computed properties without introducing significant overhead or complex APIs, making it a good choice for applications already leveraging Zustand that require derived state.
npm install zustand-computed-stateVerified import paths — ran on the pinned version, not inferred.
Demonstrates defining a Zustand store with the `zustand-computed-state` middleware to create a `countSq` derived property from `count`, showing both its definition and how it's accessed.
For slice patterns, use `...compute('my_slice_id', get, state => ({...}))`.Ensure the order of `compute` definitions if one computed property relies on another. For properties within the same `compute` block, dependencies are typically handled correctly.
Always check the `zustand-computed-state` release notes and peer dependency requirements when upgrading `zustand` to a new major version.
Ensure `import { create } from 'zustand';` is present at the top of your file.Manually add the computed properties and their types to your `StoreType` interface or type alias. For example, `type Store = { count: number; countSq: number; };`