分页
基于 IntersectionObserver 的页面/无限滚动辅助 hook.
useListPagingForSentinel
在第一个/最后一个项目上插入 ref,当进入视口时触发前一/下一页调用.
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,
});