Skip to content

Commit e8e7039

Browse files
committed
limit infinite query pages
1 parent e34baf9 commit e8e7039

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

apps/3.2-vite-reactquery-infinite/src/pages/HomePage.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import { IconAlertCircle } from "@tabler/icons-react";
1414
import { type IPost } from "../api-types";
1515
import { useInfiniteQuery } from "@tanstack/react-query";
1616

17+
const numberOfPostsToLoad = 10;
18+
const maxNumberOfPosts = 100;
19+
1720
export function HomePage() {
1821
//load posts
1922
const {
@@ -26,7 +29,7 @@ export function HomePage() {
2629
queryKey: ["posts"],
2730
queryFn: async ({ pageParam = 0 }) => {
2831
const fetchUrl = new URL(
29-
`http://localhost:3333/posts?_page=${pageParam}&_limit=10`,
32+
`http://localhost:3333/posts?_page=${pageParam}&_limit=${numberOfPostsToLoad}`
3033
);
3134

3235
const response = await fetch(fetchUrl.href);
@@ -39,7 +42,11 @@ export function HomePage() {
3942

4043
const onScroll = (e: React.UIEvent<HTMLDivElement>) => {
4144
const { scrollTop, clientHeight, scrollHeight } = e.currentTarget;
42-
if (scrollTop + clientHeight >= scrollHeight - 100 && !isFetchingPosts) {
45+
if (
46+
scrollTop + clientHeight >= scrollHeight - 100 &&
47+
!isFetchingPosts &&
48+
(posts?.pages?.length ?? 0) * numberOfPostsToLoad < maxNumberOfPosts
49+
) {
4350
fetchNextPage();
4451
}
4552
};
@@ -102,7 +109,7 @@ export function HomePage() {
102109
</Text>
103110
</Card>
104111
</Link>
105-
)),
112+
))
106113
)
107114
)}
108115
</Flex>

0 commit comments

Comments
 (0)