-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathUseInfiniteScrollHook.tsx
More file actions
72 lines (67 loc) · 1.89 KB
/
UseInfiniteScrollHook.tsx
File metadata and controls
72 lines (67 loc) · 1.89 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
import React, { useState } from 'react';
import { useInfiniteScroll } from '../index';
const itemStyle = {
height: 30,
border: '1px solid steelblue',
margin: 6,
padding: 8,
borderRadius: 4,
};
const containerStyle: React.CSSProperties = {
height: 400,
overflow: 'auto',
border: '2px solid steelblue',
borderRadius: 4,
};
export default function UseInfiniteScrollHook() {
const [items, setItems] = useState<number[]>(() =>
Array.from({ length: 20 }, (_, i) => i)
);
const [hasMore, setHasMore] = useState(true);
const { sentinelRef, isLoading } = useInfiniteScroll({
next: () => {
setTimeout(() => {
setItems((prev) => {
const next = Array.from({ length: 20 }, (_, i) => prev.length + i);
if (prev.length + next.length >= 200) setHasMore(false);
return [...prev, ...next];
});
}, 800);
},
hasMore,
dataLength: items.length,
});
return (
<div>
<h1>useInfiniteScroll hook, custom UI</h1>
<p style={{ color: '#666', fontSize: 13 }}>
Fully custom markup. The hook manages the observer; you own the layout.
</p>
<hr />
<div id="hookScrollTarget" style={containerStyle}>
<ul style={{ listStyle: 'none', margin: 0, padding: 0 }}>
{items.map((n) => (
<li key={n} style={itemStyle}>
item #{n + 1}
</li>
))}
<li
ref={sentinelRef}
aria-hidden="true"
style={{ height: 1, padding: 0 }}
/>
{isLoading && (
<li style={{ textAlign: 'center', padding: 12, color: '#888' }}>
Loading more items...
</li>
)}
{!hasMore && (
<li style={{ textAlign: 'center', padding: 12, color: '#aaa' }}>
All items loaded.
</li>
)}
</ul>
</div>
</div>
);
}