Skip to content

Commit 2b5e7f0

Browse files
committed
flip if else
1 parent d9523b6 commit 2b5e7f0

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/useOnInView.tsx

+14-13
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,27 @@ export const useOnInView = <TElement extends Element>(
7272
| undefined
7373
| ReturnType<IntersectionChangeEffect<TElement>>;
7474

75-
// enter: intersectionsStateTrigger = true
76-
// leave: intersectionsStateTrigger = false
75+
// trigger "enter": intersectionsStateTrigger = true
76+
// trigger "leave": intersectionsStateTrigger = false
7777
const intersectionsStateTrigger = trigger !== "leave";
7878

7979
const destroyInviewObserver = observe(
8080
element,
8181
(inView, entry) => {
82+
// Call the callback when the element is in view (if trigger is "enter")
83+
// Call the callback when the element is out of view (if trigger is "leave")
84+
if (inView === intersectionsStateTrigger) {
85+
callbackCleanup = onIntersectionChangeRef.current(entry);
86+
87+
// if there is no cleanup function returned from the callback
88+
// and triggerOnce is true, the observer can be destroyed immediately
89+
if (triggerOnce && !callbackCleanup) {
90+
destroyInviewObserver();
91+
}
92+
}
8293
// Call cleanup when going out of view (if trigger is "enter")
8394
// Call cleanup when going in view (if trigger is "leave")
84-
if (inView !== intersectionsStateTrigger) {
95+
else {
8596
if (callbackCleanup) {
8697
callbackCleanup(entry);
8798
callbackCleanup = undefined;
@@ -91,16 +102,6 @@ export const useOnInView = <TElement extends Element>(
91102
destroyInviewObserver();
92103
}
93104
}
94-
} else {
95-
// Call the callback when the element is in view (if trigger is "enter")
96-
// Call the callback when the element is out of view (if trigger is "leave")
97-
callbackCleanup = onIntersectionChangeRef.current(entry);
98-
99-
// if there is no cleanup function returned from the callback
100-
// and triggerOnce is true, the observer can be destroyed immediately
101-
if (triggerOnce && !callbackCleanup) {
102-
destroyInviewObserver();
103-
}
104105
}
105106
},
106107
{

0 commit comments

Comments
 (0)