mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
29 lines
857 B
TypeScript
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;
|