use-optimized-selector
Version:
A hook to memoized selector values, useful for bailing out of renders.
27 lines (26 loc) • 1.01 kB
TypeScript
/**
* A memoized or constant function in the form of:
*
* `(sourceValue) => derivedValue`
*/
export declare type Selector<Value, Return> = (value: Value) => Return;
/**
* A memoized or constant function in the form of:
*
* `(prevValue, nextValue) => boolean`
*
* Return true when objects are considered equivalent.
*/
export declare type Comparer<Return> = (prev: Return, next: Return) => boolean;
/**
* A hook that caches the value of a selector given an optional comparer.
*
* It will return the previous value when present and comparer returns true,
* bailing out of setState and consequent renders.
*
* Useful for optimizing selectors for useContextSelector, useSubscriptions, useMutableSource, etc.
*
* @param selector A memoized or constant selector
* @param comparer A memoized or constant comparer
*/
export declare const useOptimizedSelector: <Value, Return>(selector: Selector<Value, Return>, comparer?: Comparer<Return>) => Selector<Value, Return>;