-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
index.js
executable file
·77 lines (72 loc) · 1.71 KB
/
index.js
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
71
72
73
74
75
76
77
import React from 'react'
import fetch from '../libs/fetch'
import Link from 'next/link'
import { useQuery } from 'react-query'
export default () => {
const {
data,
isLoading,
isFetching,
isFetchingMore,
fetchMore,
canFetchMore,
} = useQuery(
'projects',
({ nextId } = {}) => fetch('/api/projects?cursor=' + (nextId || 0)),
{
paginated: true,
getCanFetchMore: lastPage => lastPage.nextId,
}
)
const loadMore = async () => {
try {
const { nextId } = data[data.length - 1]
await fetchMore({
nextId,
})
} catch {}
}
return (
<div>
<h1>Pagination</h1>
{isLoading ? (
<p>Loading...</p>
) : data ? (
<>
{data.map((page, i) => (
<React.Fragment key={i}>
{page.data.map(project => (
<p
style={{
border: '1px solid gray',
borderRadius: '5px',
padding: '5rem 1rem',
}}
key={project.id}
>
{project.name}
</p>
))}
</React.Fragment>
))}
<div>
{canFetchMore ? (
<button onClick={loadMore} disabled={isFetchingMore}>
{isFetchingMore ? 'Loading more...' : 'Load More'}
</button>
) : (
'Nothing more to fetch.'
)}
</div>
<div>
{isFetching && !isFetchingMore ? 'Background Updating...' : null}
</div>
</>
) : null}
<hr />
<Link href="/about">
<a>Go to another page</a>
</Link>
</div>
)
}