Paging
IntersectionObserver 기반 페이지/무한 스크롤 보조 hook입니다.
useListPagingForSentinel
첫 번째/마지막 아이템에 ref를 꽂고, viewport에 들어왔을 때 이전/다음 페이지 호출을 트리거합니다.
tsx
const { firstChildRef, lastChildRef } = useListPagingForSentinel<HTMLDivElement>({
onReachTerminal: ({ isFirst, isLast }) => {
if (isFirst) loadPreviousPage();
if (isLast) loadNextPage();
},
});
return items.map((item, index) => (
<div
key={item.id}
ref={
index === 0
? firstChildRef
: index === items.length - 1
? lastChildRef
: undefined
}
>
{item.name}
</div>
));usePaginationViewNumber
showCurrentPageNumber
현재 보이는 페이지 번호입니다.
showCurrentPageObserveTarget
data-page-index를 가진 페이지 wrapper에 연결하는 observe ref입니다.
usePagingHandler
usePagingHandler는 이미 로드된 페이지로는 scrollIntoView만 수행하고, 없는 페이지는 fetch callback을 호출하도록 도와줍니다.
tsx
const dataListRef = useRef<Array<Item[] | null>>([]);
const { setPaginationRef, handleReachTerminal, handleClickPageChange } =
usePagingHandler<Item>({
lastCallPageNumber: 0,
dataListRef,
});