Skip to content

Commit 831ca3a

Browse files
authored
refactor: change the Map loop to forEach (#200)
* refactor: change the Map loop to forEach Using for...of created a lot of babel bloat, while requiring Symbol.iterator to be defined. * Sync with Prettier
1 parent 466cd77 commit 831ca3a

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

src/intersection.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,17 @@ export function unobserve(element: Element | null) {
112112
let rootObserved = false
113113
/* istanbul ignore else */
114114
if (observerId) {
115-
for (let [key, item] of INSTANCE_MAP) {
115+
INSTANCE_MAP.forEach((item, key) => {
116116
if (key !== element) {
117117
if (item.observerId === observerId) {
118118
itemsLeft = true
119119
rootObserved = true
120-
break
121120
}
122121
if (item.observer.root === root) {
123122
rootObserved = true
124123
}
125124
}
126-
}
125+
})
127126
}
128127
if (!rootObserved && root) ROOT_IDS.delete(root)
129128
if (observer && !itemsLeft) {

src/useInView.tsx

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,35 @@ export function useInView(options: IntersectionOptions = {}): HookResponse {
1515
entry: undefined,
1616
})
1717

18-
React.useEffect(() => {
19-
if (!ref) return
20-
observe(
21-
ref,
22-
(inView, intersection) => {
23-
setState({ inView, entry: intersection })
18+
React.useEffect(
19+
() => {
20+
if (!ref) return
21+
observe(
22+
ref,
23+
(inView, intersection) => {
24+
setState({ inView, entry: intersection })
2425

25-
if (inView && options.triggerOnce) {
26-
// If it should only trigger once, unobserve the element after it's inView
27-
unobserve(ref)
28-
}
29-
},
30-
options,
31-
)
26+
if (inView && options.triggerOnce) {
27+
// If it should only trigger once, unobserve the element after it's inView
28+
unobserve(ref)
29+
}
30+
},
31+
options,
32+
)
3233

33-
return () => {
34-
unobserve(ref)
35-
}
36-
}, [
37-
// Only create a new Observer instance if the ref or any of the options have been changed.
38-
ref,
39-
options.threshold,
40-
options.root,
41-
options.rootMargin,
42-
options.triggerOnce,
43-
])
34+
return () => {
35+
unobserve(ref)
36+
}
37+
},
38+
[
39+
// Only create a new Observer instance if the ref or any of the options have been changed.
40+
ref,
41+
options.threshold,
42+
options.root,
43+
options.rootMargin,
44+
options.triggerOnce,
45+
],
46+
)
4447

4548
React.useDebugValue(state.inView)
4649

0 commit comments

Comments
 (0)