-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuseEffect.js
More file actions
31 lines (30 loc) · 825 Bytes
/
useEffect.js
File metadata and controls
31 lines (30 loc) · 825 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
29
30
31
function useEffect(func, deps) {
// If the document object is defined (i.e., if this code is running in a browser environment)
if (typeof document !== "undefined") {
let run = false;
// Check if any of the dependencies have changed
if (deps.length === 0) {
if (variables.emptyEffectRan === undefined) {
variables.emptyEffectRan = false;
}
if (!variables.emptyEffectRan) {
run = true;
variables.emptyEffectRan = true;
}
} else {
deps.forEach((a) => {
if (
effectVariables[a] !== variables[a] &&
effectVariables[a] !== undefined
) {
run = true;
}
});
}
// If any of the dependencies have changed, call the function
if (run) {
func();
}
}
}
module.exports = useEffect;