@supunlakmal/hooks
Version:
A collection of reusable React hooks
28 lines (27 loc) • 2.07 kB
TypeScript
import { type DependencyList } from 'react';
export type DependenciesComparator<Deps extends DependencyList = DependencyList> = (a: Deps, b: Deps) => boolean;
export type Predicate = (previous: any, next: any) => boolean;
export type ConditionsList = readonly any[];
export type ConditionsPredicate<Cond extends ConditionsList = ConditionsList> = (conditions: Cond) => boolean;
export declare const noop: () => void;
export declare const isBrowser: boolean;
export declare const isStrictEqual: Predicate;
export declare const truthyAndArrayPredicate: ConditionsPredicate;
export declare const truthyOrArrayPredicate: ConditionsPredicate;
export type EffectCallback = () => void | (() => void | undefined);
export type EffectHook<Callback extends EffectCallback = EffectCallback, Deps extends DependencyList = DependencyList, HookRestArgs extends any[] = any[]> = (...args: [Callback, Deps, ...HookRestArgs]) => void;
export declare const basicDepsComparator: DependenciesComparator;
/**
* Like `useEffect` but uses provided comparator function to validate dependency changes.
*
* @param callback Function that will be passed to the underlying effect hook.
* @param deps Dependency list like the one passed to `useEffect`.
* @param comparator Function that compares two dependency arrays,
* and returns `true` if they're equal.
* @param effectHook Effect hook that will be used to run
* `callback`. Must match the type signature of `useEffect`, meaning that the `callback` should be
* placed as the first argument and the dependency list as second.
* @param effectHookRestArgs Extra arguments that are passed to the `effectHook`
* after the `callback` and the dependency list.
*/
export declare const useCustomCompareEffect: <Callback extends EffectCallback = EffectCallback, Deps extends DependencyList = DependencyList, HookRestArgs extends any[] = any[], R extends HookRestArgs = HookRestArgs>(callback: Callback, deps: Deps, comparator?: DependenciesComparator<Deps>, effectHook?: EffectHook<Callback, Deps, HookRestArgs>, ...effectHookRestArgs: R) => void;