Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
zustand-middleware-computed-state
Advanced tools
Readme
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))
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>
)
}
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;
FAQs
Computed state middleware for Zustand
The npm package zustand-middleware-computed-state receives a total of 1,056 weekly downloads. As such, zustand-middleware-computed-state popularity was classified as popular.
We found that zustand-middleware-computed-state demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.