forked from TanStack/virtual
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tsx
More file actions
161 lines (143 loc) · 4.04 KB
/
main.tsx
File metadata and controls
161 lines (143 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import React from 'react'
import ReactDOM from 'react-dom'
import axios from 'axios'
import { QueryClient, QueryClientProvider, useInfiniteQuery } from 'react-query'
import './index.css'
import { useVirtualizer } from '@tanstack/react-virtual'
const queryClient = new QueryClient()
async function fetchServerPage(
limit: number,
offset: number = 0,
): Promise<{ rows: string[]; nextOffset: number }> {
const rows = new Array(limit)
.fill(0)
.map((e, i) => `Async loaded row #${i + offset * limit}`)
await new Promise((r) => setTimeout(r, 500))
return { rows, nextOffset: offset + 1 }
}
function App() {
const {
status,
data,
error,
isFetching,
isFetchingNextPage,
fetchNextPage,
hasNextPage,
} = useInfiniteQuery(
'projects',
(ctx) => fetchServerPage(10, ctx.pageParam),
{
getNextPageParam: (_lastGroup, groups) => groups.length,
},
)
const allRows = data ? data.pages.flatMap((d) => d.rows) : []
const parentRef = React.useRef()
const rowVirtualizer = useVirtualizer({
count: hasNextPage ? allRows.length + 1 : allRows.length,
getScrollElement: () => parentRef.current,
estimateSize: () => 100,
overscan: 5,
})
React.useEffect(() => {
const [lastItem] = [...rowVirtualizer.getVirtualItems()].reverse()
if (!lastItem) {
return
}
if (
lastItem.index >= allRows.length - 1 &&
hasNextPage &&
!isFetchingNextPage
) {
fetchNextPage()
}
}, [
hasNextPage,
fetchNextPage,
allRows.length,
isFetchingNextPage,
rowVirtualizer.getVirtualItems(),
])
return (
<div>
<p>
This infinite scroll example uses React Query's useInfiniteScroll hook
to fetch infinite data from a posts endpoint and then a rowVirtualizer
is used along with a loader-row placed at the bottom of the list to
trigger the next page to load.
</p>
<br />
<br />
{status === 'loading' ? (
<p>Loading...</p>
) : status === 'error' ? (
<span>Error: {(error as Error).message}</span>
) : (
<div
ref={parentRef}
className="List"
style={{
height: `500px`,
width: `100%`,
overflow: 'auto',
}}
>
<div
style={{
height: `${rowVirtualizer.getTotalSize()}px`,
width: '100%',
position: 'relative',
}}
>
{rowVirtualizer.getVirtualItems().map((virtualRow) => {
const isLoaderRow = virtualRow.index > allRows.length - 1
const post = allRows[virtualRow.index]
return (
<div
key={virtualRow.index}
className={
virtualRow.index % 2 ? 'ListItemOdd' : 'ListItemEven'
}
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: `${virtualRow.size}px`,
transform: `translateY(${virtualRow.start}px)`,
}}
>
{isLoaderRow
? hasNextPage
? 'Loading more...'
: 'Nothing more to load'
: post}
</div>
)
})}
</div>
</div>
)}
<div>
{isFetching && !isFetchingNextPage ? 'Background Updating...' : null}
</div>
<br />
<br />
{process.env.NODE_ENV === 'development' ? (
<p>
<strong>Notice:</strong> You are currently running React in
development mode. Rendering performance will be slightly degraded
until this application is build for production.
</p>
) : null}
</div>
)
}
ReactDOM.render(
<React.StrictMode>
<QueryClientProvider client={queryClient}>
<App />
</QueryClientProvider>
</React.StrictMode>,
document.getElementById('root'),
)