Skip to content

Commit

Permalink
🐛 Fix: 밈 api 무한요청 수정 (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
elbica authored May 19, 2023
2 parents 2c9cd7b + f4c5461 commit 474a8d8
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/application/hooks/common/useIntersect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ type IntersectHandler = (entry: IntersectionObserverEntry, observer: Intersectio

export const useIntersect = (onIntersect: IntersectHandler, options?: IntersectionObserverInit) => {
const [ref, setRef] = useState<Element | null>(null);
const callbackRef = useRef(
(entries: IntersectionObserverEntry[], observer: IntersectionObserver) => {
entries.forEach((entry) => {
if (entry.isIntersecting) onIntersect(entry, observer);
});
},
const callback = (entries: IntersectionObserverEntry[], observer: IntersectionObserver) => {
entries.forEach((entry) => {
if (entry.isIntersecting) onIntersect(entry, observer);
});
};
const observerRef = useRef(
typeof IntersectionObserver === "undefined"
? undefined
: new IntersectionObserver(callback, options),
);

useEffect(() => {
if (!ref) return;
const observer = new IntersectionObserver(callbackRef.current, options);
const observer = observerRef.current;
if (!ref || !observer) return;
observer.observe(ref);
return () => observer.disconnect();
}, [ref, options]);
}, [ref]);

return setRef;
};

1 comment on commit 474a8d8

@vercel
Copy link

@vercel vercel bot commented on 474a8d8 May 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.