Skip to content

Commit e3f510b

Browse files
committed
docs: readme and examples
1 parent c35804c commit e3f510b

File tree

1 file changed

+53
-4
lines changed

1 file changed

+53
-4
lines changed

README.md

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,43 @@
88

99
React Hooks library to use classic pagination in the frontend with a token-based paginatiom backend
1010

11+
<!-- toc -->
12+
13+
<!-- tocstop -->
14+
1115
## Setup
1216

1317
`npm i token-pagination-hooks`
1418

1519
## Quickstart
1620

17-
The API you're using
21+
### API
22+
23+
See example API:
24+
25+
[![Edit server](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/server-0wmht?fontsize=14&hidenavigation=1&theme=dark)
26+
27+
Assiming you're using an API which:
1828

1929
- accepts a `pageToken` query string parameter to do pagination
2030
- returns data in the format:
2131

2232
```json
2333
{
24-
"data": [...],
34+
"data": [{
35+
"id": 1,
36+
"value": "some value"
37+
}],
2538
"nextPage": "some opaque string"
2639
}
2740
```
2841

42+
### Client
43+
44+
See example client:
45+
46+
[![Edit with axios-hooks](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/with-axios-hooks-u035y?fontsize=14&hidenavigation=1&theme=dark)
47+
2948
Assuming you're using a library like [axios-hooks](https://github.com/simoneb/axios-hooks/) to interact with the API:
3049

3150
```js
@@ -35,7 +54,37 @@ import useTokenPagination from 'token-pagination-hooks'
3554

3655
function Pagination() {
3756
const [pageNumber, setPageNumber] = useState(1)
38-
const { currentToken, useUpdateToken } = useTokenPagination(pageNumber)
39-
const [{ data }] = useAxios({ url: '/api', params: { pageToken: currentToken })
57+
const { currentToken, useUpdateToken, hasToken } = useTokenPagination(
58+
pageNumber
59+
)
60+
const [{ data }] = useAxios({
61+
url: '/api',
62+
params: { pageSize: 3, pageToken: currentToken }
63+
})
64+
65+
useUpdateToken(data?.nextPage)
66+
67+
return (
68+
<>
69+
<pre>{JSON.stringify(data, null, 2)}</pre>
70+
<div>
71+
<button
72+
disabled={pageNumber <= 1}
73+
onClick={() => setPageNumber((p) => p - 1)}
74+
>
75+
&lt;&lt;
76+
</button>
77+
{' '}
78+
{pageNumber}
79+
{' '}
80+
<button
81+
disabled={!hasToken(pageNumber + 1) || !data?.nextPage}
82+
onClick={() => setPageNumber((p) => p + 1)}
83+
>
84+
&gt;&gt;
85+
</button>
86+
</div>
87+
</>
88+
)
4089
}
4190
```

0 commit comments

Comments
 (0)