upgrade useCustomCompareEffect.ts

Added generic useCustomCompareEffect for dependencies inference in compare function
This commit is contained in:
Alexey Bojhev 2020-01-10 03:57:56 +03:00 committed by GitHub
parent e232bcc6fe
commit 091c9077ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,9 +2,16 @@ import { DependencyList, EffectCallback, useEffect, useRef } from 'react';
const isPrimitive = (val: any) => val !== Object(val);
type DepsEqualFnType = (prevDeps: DependencyList, nextDeps: DependencyList) => boolean;
type DepsEqualFnType<TDeps extends DependencyList> = (
prevDeps: TDeps,
nextDeps: TDeps
) => boolean;
const useCustomCompareEffect = (effect: EffectCallback, deps: DependencyList, depsEqual: DepsEqualFnType) => {
const useCustomCompareEffect = <TDeps extends DependencyList>(
effect: EffectCallback,
deps: TDeps,
depsEqual: DepsEqualFnType<TDeps>
) => {
if (process.env.NODE_ENV !== 'production') {
if (!(deps instanceof Array) || !deps.length) {
console.warn('`useCustomCompareEffect` should not be used with no dependencies. Use React.useEffect instead.');
@ -21,7 +28,7 @@ const useCustomCompareEffect = (effect: EffectCallback, deps: DependencyList, de
}
}
const ref = useRef<DependencyList | undefined>(undefined);
const ref = useRef<TDeps | undefined>(undefined);
if (!ref.current || !depsEqual(deps, ref.current)) {
ref.current = deps;