createEffect
Creates an effect that runs when the store’s state changes.
const effect = createEffect(store, effectFn, dependencyFn?);Parameters
Section titled “Parameters”store- The store instance to subscribe toeffectFn- The function to run when dependencies change. Receives the current state as argument.dependencyFn?- Optional function that returns an array of dependencies. If provided, the effect will only run when these dependencies change.
Returns
Section titled “Returns”An Effect object with the following methods:
mount- Start listening to changes in the original store.unmount- Stop listening to changes in the original store.
import { createEffect } from "dharma-core";import { counterStore } from "./counterStore";
const countLogger = createEffect( counterStore, (state) => console.log("count =", state.count), (state) => [state.count],);
countLogger.mount();