Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

App.tsx

84 lines2.3 KB

1import { Fragment } from 'preact'
2import { useInfiniteQuery } from '@tanstack/preact-query'
3import { PreactQueryDevtools } from '@tanstack/preact-query-devtools'
4import { projectsQueries } from './services/projects/queries'
5​
6export default function App() {
7 const {
8 status,
9 data,
10 error,
11 isFetching,
12 isFetchingNextPage,
13 isFetchingPreviousPage,
14 fetchNextPage,
15 fetchPreviousPage,
16 hasNextPage,
17 hasPreviousPage,
18 } = useInfiniteQuery(projectsQueries.infinite())
19​
20 return (
21 <div>
22 <h1>Infinite Query with max pages</h1>
23 <h3>4 projects per page</h3>
24 <h3>3 pages max</h3>
25 {status === 'pending' ? (
26 <p>Loading...</p>
27 ) : status === 'error' ? (
28 <span>Error: {error.message}</span>
29 ) : (
30 <>
31 <div>
32 <button
33 onClick={() => fetchPreviousPage()}
34 disabled={!hasPreviousPage || isFetchingPreviousPage}
35 >
36 {isFetchingPreviousPage
37 ? 'Loading more...'
38 : hasPreviousPage
39 ? 'Load Older'
40 : 'Nothing more to load'}
41 </button>
42 </div>
43 {data.pages.map((page) => (
44 <Fragment key={page.nextId}>
45 {page.data.map((project) => (
46 <p
47 style={{
48 border: '1px solid gray',
49 borderRadius: '5px',
50 padding: '8px',
51 fontSize: '14px',
52 background: `hsla(${project.id * 30}, 60%, 80%, 1)`,
53 }}
54 key={project.id}
55 >
56 {project.name}
57 </p>
58 ))}
59 </Fragment>
60 ))}
61 <div>
62 <button
63 onClick={() => fetchNextPage()}
64 disabled={!hasNextPage || isFetchingNextPage}
65 >
66 {isFetchingNextPage
67 ? 'Loading more...'
68 : hasNextPage
69 ? 'Load Newer'
70 : 'Nothing more to load'}
71 </button>
72 </div>
73 <div>
74 {isFetching && !isFetchingNextPage
75 ? 'Background Updating...'
76 : null}
77 </div>
78 </>
79 )}
80 <hr />
81 <PreactQueryDevtools initialIsOpen />
82 </div>
83 )