Open History to see the last commit
1---
2id: infinite-queries
3title: Infinite Queries
4ref: docs/framework/react/guides/infinite-queries.md
5replace: { 'react-query': 'preact-query', 'React': 'Preact' }
6---
7β
8[//]: # 'Example'
9β
10```tsx
11import { useInfiniteQuery } from '@tanstack/preact-query'
12β
13function Projects() {
14 const fetchProjects = async ({ pageParam }) => {
15 const res = await fetch('/api/projects?cursor=' + pageParam)
16 return res.json()
17 }
18β
19 const {
20 data,
21 error,
22 fetchNextPage,
23 hasNextPage,
24 isFetching,
25 isFetchingNextPage,
26 status,
27 } = useInfiniteQuery({
28 queryKey: ['projects'],
29 queryFn: fetchProjects,
30 initialPageParam: 0,
31 getNextPageParam: (lastPage, pages) => lastPage.nextCursor,
32 })
33β
34 return status === 'pending' ? (
35 <p>Loading...</p>
36 ) : status === 'error' ? (
37 <p>Error: {error.message}</p>
38 ) : (
39 <>
40 {data.pages.map((group, i) => (
41 <div key={i}>
42 {group.data.map((project) => (
43 <p key={project.id}>{project.name}</p>
44 ))}
45 </div>
46 ))}
47 <div>
48 <button
49 onClick={() => fetchNextPage()}
50 disabled={!hasNextPage || isFetching}
51 >
52 {isFetchingNextPage
53 ? 'Loading more...'
54 : hasNextPage
55 ? 'Load More'
56 : 'Nothing more to load'}
57 </button>
58 </div>
59 <div>{isFetching && !isFetchingNextPage ? 'Fetching...' : null}</div>
60 </>
61 )
62}
63```
64β
65[//]: # 'Example'