ページング
IntersectionObserverベースのページ/無限スクロール補助フックです。
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コールバックを呼び出すのを助けます。
tsx
const dataListRef = useRef<Array<Item[] | null>>([]);
const { setPaginationRef, handleReachTerminal, handleClickPageChange } =
usePagingHandler<Item>({
lastCallPageNumber: 0,
dataListRef,
});