derive
Creates a derived store from an existing store.
A derived store computes its state based on the original store’s state using a derive function. The derived store’s value is memoized and only recomputed when necessary.
const derivedStore = derive(store, deriveFn, dependencyFn?);Parameters
Section titled “Parameters”store- The original store to derive fromderiveFn- A function that computes the derived state from the original statedependencyFn?- Optional function that returns an array of dependencies. If provided, the derived state will only be recomputed when these dependencies change
Returns
Section titled “Returns”A new store containing the derived state.
Actions
mount- Start listening to changes in the original store. Called automatically on the first subscription.unmount- Stop listening to changes in the original store. Called automatically when there are no more subscribers.
import { derive } from "dharma-core";import { counterStore } from "./counterStore";
export const doubleCounterStore = derive( counterStore, (state) => state.count * 2, (state) => [state.count],);