File tree Expand file tree Collapse file tree 1 file changed +10
-3
lines changed
apps/3.2-vite-reactquery-infinite/src/pages Expand file tree Collapse file tree 1 file changed +10
-3
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,9 @@ import { IconAlertCircle } from "@tabler/icons-react";
1414import { type IPost } from "../api-types" ;
1515import { useInfiniteQuery } from "@tanstack/react-query" ;
1616
17+ const numberOfPostsToLoad = 10 ;
18+ const maxNumberOfPosts = 100 ;
19+
1720export 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 >
You can’t perform that action at this time.
0 commit comments