{ // 검색어가 없거나 검색을 처음 시작할 때는 검색 X if (!keyword.trim()) { return; } const observer = new IntersectionObserver(onIntersection, { threshold: 1 }); if (loadMoreRef.current) { observer.observe(loadMoreRef.current); } return () => { if (loadMoreRef.current) { observer.unobserve(loadMoreRef.current); } }; }, [onIntersection, keyword]);"> { // 검색어가 없거나 검색을 처음 시작할 때는 검색 X if (!keyword.trim()) { return; } const observer = new IntersectionObserver(onIntersection, { threshold: 1 }); if (loadMoreRef.current) { observer.observe(loadMoreRef.current); } return () => { if (loadMoreRef.current) { observer.unobserve(loadMoreRef.current); } }; }, [onIntersection, keyword]);"> { // 검색어가 없거나 검색을 처음 시작할 때는 검색 X if (!keyword.trim()) { return; } const observer = new IntersectionObserver(onIntersection, { threshold: 1 }); if (loadMoreRef.current) { observer.observe(loadMoreRef.current); } return () => { if (loadMoreRef.current) { observer.unobserve(loadMoreRef.current); } }; }, [onIntersection, keyword]);">
const onIntersection = useCallback(
(entries) => {
console.log("onIntersection function");
const firstEntity = entries[0];
if (firstEntity.isIntersecting && hasMore) {
fetchSearchPlaces(searchType, keyword, page);
}
},
[hasMore, searchType, keyword, page]
);
useEffect(() => {
// 검색어가 없거나 검색을 처음 시작할 때는 검색 X
if (!keyword.trim()) {
return;
}
const observer = new IntersectionObserver(onIntersection, { threshold: 1 });
if (loadMoreRef.current) {
observer.observe(loadMoreRef.current);
}
return () => {
if (loadMoreRef.current) {
observer.unobserve(loadMoreRef.current);
}
};
}, [onIntersection, keyword]);
const observer = new IntersectionObserver(callback, options);
useEffect(() => {
// 검색어가 없거나 검색을 처음 시작할 때는 검색 X
if (!keyword.trim()) {
return;
}
const observer = new IntersectionObserver(onIntersection, { threshold: 1 });
if (loadMoreRef.current) {
observer.observe(loadMoreRef.current);
}
return () => {
if (loadMoreRef.current) {
observer.unobserve(loadMoreRef.current);
//observer.disconnect();
}
};
}, [onIntersection, keyword]);
observer.observe(loadMoreRef.current);
observer.observe(loadMoreRef.current)
가 실행되면 observe내에 파라미터 loadMoreRef.current
요소가 관찰되기 시작한다.observer.unobserve(loadMoreRef.current);
// Intersection Observer의 callback 함수
const onIntersection = useCallback(
(entries) => {
console.log("onIntersection function");
const firstEntity = entries[0];
if (firstEntity.isIntersecting && hasMore) {
fetchSearchPlaces(searchType, keyword, page);
}
},
[hasMore, searchType, keyword, page]
);
entries
파라미터는 관찰중인 요소들에 대한 정보를 담은 배열이다.
배열의 각 항목은 IntersectionObserverEntry
객체이다.entries
는 loadMoreRef.current
라는 **“하나”**의 관찰요소만 포함하고있기때문에 entires[0]
를 꺼내도록 하였다.IntersectionObserverEntry
의 주요속성
isIntersecting
intersectionRatio
:
0.5
면 요소가 50% 화면에 보임.target
: