/*! * ======================================================================== * @rzl-zone/core-react * ------------------------------------------------------------------------ * Version: `0.0.14` * Author: `Rizalvin Dwiky ` * Repository: `https://github.com/rzl-zone/rzl-zone/tree/main/packages/core-react` * ======================================================================== */ /** -------------------------------------------------------------------------- * * ***Run a side-effect when a value changes (manual change detection).*** * -------------------------------------------------------------------------- * * A lightweight hook to detect changes between renders and execute * a callback **only when the value is considered "updated"**. * * --- * * - ***Core behavior:*** * - Stores the previous value internally. * - Compares `previous` and `current` values on every render. * - Executes `onChange(current, previous)` **synchronously** * when `isUpdated(previous, current)` returns `true`. * * --- * * - ⚠️ ***Important notes:*** * - This hook **does NOT use `useEffect`**. * - The comparison and callback run during render. * - Make sure `onChange` does **not cause infinite re-renders**. * * --- * * - ***Default comparison strategy:*** * - Uses a shallow `!==` comparison. * - Supports basic deep comparison for arrays. * * --- * * - ***Designed for:*** * - Lightweight change tracking. * - Controlled state synchronization. * - Custom state diffing logic without `useEffect`. * * --- * * @template T - The watched value type. * * @param value * The current value to observe. * * @param onChange * Callback invoked when the value is considered changed. * * Receives: * - `current` — the new value. * - `previous` — the previous value. * * @param isUpdated * Optional custom comparison function. * * Determines whether the value has changed. * Defaults to a shallow comparison with array support. * * --- * * @example * **1. Detect primitive value changes.** * ```tsx * const [count, setCount] = useState(0); * * useOnChange(count, (current, prev) => { * console.log("Count changed:", prev, "➔", current); * }); * ``` * * @example * **2. Detect object changes with custom comparator.** * ```tsx * type User = { id: number; name: string }; * * useOnChange( * user, * (current, prev) => { * console.log("User updated", prev, current); * }, * (prev, current) => prev.id !== current.id * ); * ``` * * @example * **3. Sync external state without `useEffect`.** * ```tsx * useOnChange(value, (current) => { * localStorage.setItem("value", JSON.stringify(current)); * }); * ``` */ declare function useOnChange(value: T, onChange: (current: T, previous: T) => void, isUpdated?: (prev: T, current: T) => boolean): void; export { useOnChange };