react-use/src/useDeepCompareEffect.ts

29 lines
857 B
TypeScript

import { DependencyList, EffectCallback, useEffect, useRef } from 'react';
import isEqual from 'react-fast-compare';
const isPrimitive = (val: any) => val !== Object(val);
const useDeepCompareEffect = (effect: EffectCallback, deps: any[]) => {
if (process.env.NODE_ENV !== 'production') {
if (!deps || !deps.length) {
console.warn('`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead.');
}
if (deps.every(isPrimitive)) {
console.warn(
'`useDeepCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead.'
);
}
}
const ref = useRef<DependencyList | undefined>(undefined);
if (!isEqual(deps, ref.current)) {
ref.current = deps;
}
useEffect(effect, ref.current);
};
export default useDeepCompareEffect;