zustand-middleware-computed-state
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

Zustand Computed State Middleware

This is a dead simple middleware for adding computed state to state management library Zustand.

npm install zustand-middleware-computed-state
-- or --
yarn add zustand-middleware-computed-state

The computed values are defined as a function passed as the second argument passed into the computed(store, computedStore) middleware. The resulting values will be merged into the store and accessible just like any other bit of Zustand state.

Since this computed state is updated on every state change, these should be kept light.

import create from 'zustand'
import { computed } from 'zustand-middleware-computed-state'

const useState = create(computed(store, computedStore))

Example

import create from 'zustand'
import { computed } from 'zustand-middleware-computed-state'

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> {/* output: 1*/}
      <div>computedCount: {computedCount}</div> {/* output: 11*/}
      <div>isEnough: {isEnough}</div> {/* output: "Is not enough*/}
    </div>
  )
}

TypeScript

code below demonstrates how you can use it with Typescript:

import { create } from "zustand";
import { computed } from "zustand-middleware-computed-state";

type ComputedStore = {
  sum: number;
};

type Store = {
  x: number;
  y: number;
  incX: (by: number) => void
  incY: (by: number) => void
};

type SetType = (
  partial:
    | Store
    | Partial<Store>
    | ((state: Store) => Store | Partial<Store>),
  replace?: boolean | undefined
) => void;

type CombinedStore = State & ComputedStore;

function computedState(state: Store): ComputedStore {
  return {
    sum: state.x + state.y,
  };
}

const useSampleStore = create<CombinedStore>(
  computed<Store, ComputedStore>(
    (set: SetType) => ({
      x: 0,
      y: 0,
      incX: (by) => set(store => ({x: state.x + by})),
      incY: (by) => set(store => ({y: state.y + by}))
    }),
    computedState
  )
);

export default useSampleStore;

Package Sidebar

Install

npm i zustand-middleware-computed-state

Weekly Downloads

630

Version

0.1.3

License

MIT

Unpacked Size

7.77 kB

Total Files

7

Last publish

Collaborators

  • cmlarsen