Socket
Socket
Sign inDemoInstall

zustand-middleware-computed-state

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

zustand-middleware-computed-state

Computed state middleware for Zustand


Version published
Maintainers
1
Weekly downloads
784
decreased by-8.62%

Weekly downloads

Readme

Source

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;

Keywords

FAQs

Last updated on 24 May 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc