MCPcopy Index your code
hub / github.com/TanStack/virtual / App

Function App

examples/react/infinite-scroll/src/main.tsx:28–154  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

26}
27
28function App() {
29 const {
30 status,
31 data,
32 error,
33 isFetching,
34 isFetchingNextPage,
35 fetchNextPage,
36 hasNextPage,
37 } = useInfiniteQuery({
38 queryKey: ['projects'],
39 queryFn: (ctx) => fetchServerPage(10, ctx.pageParam),
40 getNextPageParam: (lastGroup) => lastGroup.nextOffset,
41 initialPageParam: 0,
42 })
43
44 const allRows = data ? data.pages.flatMap((d) => d.rows) : []
45
46 const parentRef = React.useRef<HTMLDivElement>(null)
47
48 const rowVirtualizer = useVirtualizer({
49 count: hasNextPage ? allRows.length + 1 : allRows.length,
50 getScrollElement: () => parentRef.current,
51 estimateSize: () => 100,
52 overscan: 5,
53 })
54
55 React.useEffect(() => {
56 const [lastItem] = [...rowVirtualizer.getVirtualItems()].reverse()
57
58 if (!lastItem) {
59 return
60 }
61
62 if (
63 lastItem.index >= allRows.length - 1 &&
64 hasNextPage &&
65 !isFetchingNextPage
66 ) {
67 fetchNextPage()
68 }
69 }, [
70 hasNextPage,
71 fetchNextPage,
72 allRows.length,
73 isFetchingNextPage,
74 rowVirtualizer.getVirtualItems(),
75 ])
76
77 return (
78 <div>
79 <p>
80 This infinite scroll example uses React Query's useInfiniteScroll hook
81 to fetch infinite data from a posts endpoint and then a rowVirtualizer
82 is used along with a loader-row placed at the bottom of the list to
83 trigger the next page to load.
84 </p>
85

Callers

nothing calls this directly

Calls 2

useVirtualizerFunction · 0.90
fetchServerPageFunction · 0.70

Tested by

no test coverage detected