-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathuseDeepCompareEffect.ts
More file actions
28 lines (22 loc) · 941 Bytes
/
useDeepCompareEffect.ts
File metadata and controls
28 lines (22 loc) · 941 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { DependencyList, EffectCallback } from "react";
import useCustomCompareEffect from "./useCustomCompareEffect";
import isDeepEqual from "./misc/isDeepEqual";
const isPrimitive = (val: any) => val !== Object(val);
const useDeepCompareEffect = (effect: EffectCallback, deps: DependencyList) => {
if (process.env.NODE_ENV !== "production") {
if (!(deps instanceof Array) || !deps.length) {
console.warn(
"`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead."
);
}
const allPrimitive = deps.every(isPrimitive);
const hasSingleDep = deps.length === 1;
if (allPrimitive && !hasSingleDep) {
console.warn(
"`useDeepCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead."
);
}
}
useCustomCompareEffect(effect, deps, isDeepEqual);
};
export default useDeepCompareEffect;