@n0n3br/react-use-deep-compare-effect
Version:
React hook similar to useEffect but with deep comparison for dependencies.
26 lines (25 loc) • 1.14 kB
TypeScript
import type { DependencyList } from "react";
/**
* Performs a deep comparison between two values.
* @param val1 The first value.
* @param val2 The second value.
* @returns `true` if the values are deeply equal, `false` otherwise.
*/
export declare function isDeepEqual(val1: any, val2: any): boolean;
/**
* Compares two dependency arrays, using deep comparison for objects/arrays
* and reference comparison for functions.
*/
export declare function areDepsEqualManually(// Exporting for testing purposes
prevDeps: DependencyList | undefined, nextDeps: DependencyList): boolean;
/**
* React hook that behaves like `useEffect`, but performs a deep comparison
* of dependencies instead of a shallow reference comparison.
*
* @param callback Function to be executed when dependencies change.
* Can return a cleanup function.
* @param dependencies Dependency array. The callback is executed if any
* dependency changes deeply (except functions, which are
* compared by reference).
*/
export declare function useDeepCompareEffect(callback: () => void | (() => void), dependencies: DependencyList): void;