UNPKG

@known-as-bmf/react-when-hooks

Version:

Reack hook toolkit to trigger useEffect, useLayoutEffect, useCallback and useMemo when a condition on dependencies is met.

35 lines (34 loc) 2.1 kB
import { DependencyList, EffectCallback } from 'react'; import { MatchFunction } from './types'; /** * `useEffect` with a predicate that all dependencies must match in order to be triggered. * @param effect The effect function to call when all the dependencies match the given predicate. * @param match The predicate function that all dependencies must match. * @param deps The dependency array. */ export declare function useEffectWhen(effect: EffectCallback, match: MatchFunction, deps: DependencyList): void; /** * `useLayoutEffect` with a predicate that all dependencies must match in order to be triggered. * @param effect The effect function to call when all the dependencies match the given predicate. * @param match The predicate function that all dependencies must match. * @param deps The dependency array. */ export declare function useLayoutEffectWhen(effect: EffectCallback, match: MatchFunction, deps: DependencyList): void; /** * `useMemo` with a predicate that all dependencies must match in order to generate the value. * @param factory The value generating function to call when all the dependencies match the given predicate. * @param match The predicate function that all dependencies must match. * @param deps The dependency array. * * @returns The value generated by the factory if all dependencies match the predicate, `undefined` otherwise. */ export declare function useMemoWhen<T extends any>(factory: () => T, match: MatchFunction, deps: DependencyList): T | undefined; /** * `useCallback` with a predicate that all dependencies must match in order to generate the callback. * @param callback The callback to create when all the dependencies match the given predicate. * @param match The predicate function that all dependencies must match. * @param deps The dependency array. * * @returns The callback if all dependencies match the predicate, `undefined` otherwise. */ export declare function useCallbackWhen<T extends (...args: any[]) => any>(callback: T, match: MatchFunction, deps: DependencyList): T | undefined;