import { useVirtualizer } from '@tanstack/react-virtual'; import { FC, ReactElement, useRef, useState } from 'react'; import { Base } from './Base'; interface InfiniteScrollProps { rows: T[]; overscan?: number; scrollToBottom?: boolean; rowRender: (row: T) => ReactElement; } export const InfiniteScroll: FC = props => { const { rows = [], overscan = 5, scrollToBottom = false, rowRender = null } = props; const [ scrollIndex, setScrollIndex ] = useState(rows.length - 1); const parentRef = useRef(null); const virtualizer = useVirtualizer({ count: rows.length, overscan, getScrollElement: () => parentRef.current, estimateSize: () => 45, }); const items = virtualizer.getVirtualItems(); return (
{ items.map((virtualRow) => (
{ rowRender(rows[virtualRow.index]) }
)) }
); };