Registry / web-framework / zustand-middleware-computed-state

zustand-middleware-computed-state

JSON →
library0.1.3jsnpmunverified

Zustand Computed State Middleware is a lightweight utility designed to seamlessly integrate derived or computed state into Zustand stores. It enables developers to define functions that calculate new state values based on the existing store state, automatically merging these computed values back into the primary store. This allows computed properties to be accessed directly alongside regular state, simplifying state management logic. The current stable version of this specific middleware is 0.1.3, representing an initial release focused on core functionality and ease of use. However, a different package with similar functionality, `zustand-computed`, is at version 2.1.1 and has seen more active development and breaking changes. As a middleware, its release cadence will likely follow its parent library, Zustand, or be driven by feature enhancements and bug fixes. A key differentiator is its 'dead simple' approach, providing direct state augmentation without the need for manual selectors in many cases, though it explicitly advises keeping computed functions light due to their re-evaluation on every state change to prevent performance bottlenecks. It ships with full TypeScript support, requiring explicit type definitions for robust usage.

npm install zustand-middleware-computed-state
INSTALL
IMPORT
SIG · ZUSTAND-MIDDLEWARE
Z
zustand-middleware-computed-state
web-frameworkjavascriptv0.1.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

computed
import { computed } from 'zustand-middleware-computed-state'
const { computed } = require('zustand-middleware-computed-state')
This is the primary named export for the middleware. While CJS bundles may exist, the package primarily targets ESM environments.
create
import { create } from 'zustand'
import create from 'zustand'
This is Zustand's core `create` function, essential for defining stores. Modern Zustand versions (v4+) typically use named imports for `create`, whereas older versions or some configurations might default import. Zustand v5 drops default exports.
StateCreator
import type { StateCreator } from 'zustand'
import { StateCreator } from 'zustand'
When working with TypeScript and Zustand middleware, `StateCreator` is a commonly used type from Zustand itself to explicitly define the shape of your store's `set` and `get` functions and overall state, enhancing type safety.

Demonstrates how to create a Zustand store with computed state using the middleware and use it in a React component, showcasing basic increment and derived values.

import create from 'zustand'; import { computed } from 'zustand-middleware-computed-state'; import React from 'react'; import ReactDOM from 'react-dom/client'; const useStore = create( computed( (set) => ({ count: 0, inc: () => set((state) => ({ count: state.count + 1 })), }), (state) => { function isEnough() { if (state.count > 100) { return 'Is enough'; } else { return 'Is not enough'; } } return { computedCount: state.count + 10, isEnough: isEnough(), }; } ) ); function Counter() { const { count, computedCount, isEnough, inc } = useStore(); return ( <div> <button onClick={inc}>Increment</button> <div>count: {count}</div> <div>computedCount: {computedCount}</div> <div>isEnough: {isEnough}</div> </div> ); } // Render the Counter component to a DOM element const root = ReactDOM.createRoot(document.getElementById('root')); root.render(<Counter />);
Debug
Known issues
gotchaComputed values are re-evaluated on every state change. To prevent performance bottlenecks, ensure that computed functions are kept lightweight and perform minimal computations.
fix
Profile your application to identify expensive computed functions. Consider optimizing their logic or, for specific use cases, using Zustand's built-in selectors with memoization (`useCallback`) if a computed value is only needed in particular components and doesn't need to be part of the global store.
affects: >=0.1.0
gotchaWhen using TypeScript, explicit and correct type declarations are crucial for the base store, computed store, and their combined type to ensure full type safety throughout your application.
fix
Refer to the TypeScript example in the package's README, ensuring your `create` function correctly applies generics like `create<CombinedStore>(computed<Store, ComputedStore>(...))`.
affects: >=0.1.0
deprecatedThe original `zustand-middleware-computed-state` package seems to have been less actively maintained since its initial release. A package with a similar name, `zustand-computed`, has actively developed and introduced a breaking change in v2.0.0, replacing the `computed` middleware with a `createComputed` function.
fix
Consider migrating to the more actively maintained `zustand-computed` package if you require recent features or bug fixes, being mindful of the API changes, specifically the move from `computed` to `createComputed` in its v2.0.0 release.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: (0 , zustand__WEBPACK_IMPORTED_MODULE_0__.create) is not a function
This typically occurs due to incorrect module import of `create` from 'zustand', often stemming from CommonJS/ESM interop issues or version differences in Zustand.
fix
Ensure you are using the named import `import { create } from 'zustand'` for modern Zustand versions. If you are in a CommonJS environment, check your bundler configuration to correctly handle ESM modules.
Property 'someComputedValue' does not exist on type 'BaseStoreType'.
In TypeScript, this error indicates that the type of your store (`BaseStoreType` in this example) does not include the computed properties. The combined store type, including computed state, needs to be explicitly used.
fix
Define a `CombinedStore` type that merges your base store and computed store types (e.g., `type CombinedStore = BaseStore & ComputedStore;`). Then, pass this `CombinedStore` type as a generic to Zustand's `create` function: `create<CombinedStore>(...)`.
Upgrade
Version history
0.1.3latest on npm
Audit
Dependencies
zustandrequiredThis is a middleware designed for Zustand stores, requiring Zustand's core functionality.
Agent activity
47 hits · last 30 days
node
40
OpenAI (training)
1
Resources
zustand-middleware-computed-state — npm install zustand-middleware-computed-state · libregistry