Files
eewing cf73cd3b4c INIT
2026-02-17 14:10:16 -06:00

15 lines
662 B
JavaScript

import { watch } from "runed";
/**
* Simple helper function to sync a read-only dependency with writable state. This only syncs
* in one direction, from the dependency to the state. If you need to sync both directions, you
* should use the `box.with(() => dep, (v) => (dep = v))` pattern.
*
* @param getDep - A getter that returns the value that may change and whose new value will be
* passed to the `onChange` function passed as the second argument.
* @param onChange - A function that accepts a new value to react to or keep other state in sync
* with the dependency.
*/
export function useOnChange(getDep, onChange) {
watch(getDep, onChange);
}