-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfiniteScroll.tsx
More file actions
70 lines (64 loc) · 1.94 KB
/
InfiniteScroll.tsx
File metadata and controls
70 lines (64 loc) · 1.94 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
import React, { useEffect, useRef, useState, useId } from "react";
type Post = {
id: number;
title: string;
body: string;
};
export default function InfiniteScroll() {
const [data, setData] = useState<Post[]>([]);
const [page, setPage] = useState(1);
const [hasMore, setHasMore] = useState(true);
const [loading, setLoading] = useState(false);
const observerRef = useRef<HTMLDivElement | null>(null);
const limit = 10;
useEffect(() => {
const fetchData = async () => {
setLoading(true);
try {
const result = await fetch(
`https://jsonplaceholder.typicode.com/posts?_page=${page}&_limit=${limit}`
);
const json = await result.json();
setData((prev) => [...prev, ...json]);
setHasMore(json.length === limit);
} catch (e) {
console.error(e);
setHasMore(false);
} finally {
setLoading(false);
}
};
fetchData();
}, [page]);
useEffect(() => {
if (!observerRef.current || !hasMore) return;
const observer = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting && !loading) {
setPage((prev) => prev + 1);
}
},
{ threshold: 1 }
);
observer.observe(observerRef.current);
return () => observer.disconnect();
}, [loading, hasMore]);
return (
<div className="p-4 max-w-xl mx-auto">
<h1 className="text-2xl font-bold mb-4">Infinite Posts</h1>
<ul className="space-y-4">
{data.map((post, i) => (
<li key={i} className="border p-3 rounded shadow">
<h2 className="text-lg font-semibold">{post.title}</h2>
<p>
{post.id} {post.body}
</p>
</li>
))}
</ul>
{loading && <p className="mt-4 text-gray-500">Loading more...</p>}
{!hasMore && <p className="mt-4 text-gray-400">No more posts.</p>}
<div ref={observerRef} className="h-1" />
</div>
);
}