You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
React Hooks library to use classic pagination in the frontendwith a token-based paginatiom backend
9
+
React Hooks library to use classic pagination in a frontend, based on page number and page size, with a token-based paginatiom backend.
10
10
11
11
<!-- toc -->
12
12
@@ -19,19 +19,26 @@ React Hooks library to use classic pagination in the frontend with a token-based
19
19
20
20
## Setup
21
21
22
-
`npm i token-pagination-hooks`
22
+
```bash
23
+
npm i token-pagination-hooks
24
+
```
23
25
24
26
## Quickstart
25
27
26
-
### API
28
+
The hook can work in `controlled` and `uncontrolled` modes, as is the React convention. See more details in the [usage](#usage) section. This example uses the controlled mode.
- accepts a `pageToken` query string parameter to do pagination
37
+
38
+
```bash
39
+
GET /api?pageSize=2&pageToken=some-opaque-string
40
+
```
41
+
35
42
- returns data in the format:
36
43
37
44
```json
@@ -40,33 +47,33 @@ Assiming you're using an API which:
40
47
"id": 1,
41
48
"value": "some value"
42
49
}],
43
-
"nextPage": "someopaquestring"
50
+
"nextPage": "some-opaque-string"
44
51
}
45
52
```
46
53
47
-
### Client
48
-
49
-
See example client:
54
+
### Frontend
50
55
51
56
[](https://codesandbox.io/s/with-axios-hooks-u035y?fontsize=14&hidenavigation=1&theme=dark)
52
57
53
58
Assuming you're using a library like [axios-hooks](https://github.com/simoneb/axios-hooks/) to interact with the API:
Whether to reset the page number when the page size changes.
148
+
149
+
-`persister`-`object`-**Optional**
150
+
151
+
An optional persister to store the pagination state for later retrieval. Some persisters are provided with the library and others can be implemented by providing an object (or an instance of a class) adhering to the following interface:
152
+
153
+
-`persister.hydrate`-`() => object`
154
+
155
+
Method that is called to read the pagination data from the persistent store.
156
+
157
+
-`persister.persist`-`object => void`
158
+
159
+
Method that is called with an object representing the pagination state and which should persist it to the persistent store for later retrieval.
160
+
161
+
-`result`-`object`
162
+
163
+
The return value of the Hook, its properties change depending on whether `controlled` or `uncontrolled` mode is used.
164
+
165
+
**Both controlled and uncontrolled**
166
+
167
+
-`result.currentToken`-`any`
168
+
169
+
The pagination token for the requested page to provide to the API.
170
+
171
+
-`result.useUpdateToken`-`token: any => void`
172
+
173
+
The Hook to invoke with the pagination token as returned by the APIfor declarative storage of the mapping between page numbers and tokens.
174
+
175
+
-`result.updateToken`-`token: any => void`
176
+
177
+
The function to invoke with the pagination token as returned by the API for imperative storage of the mapping between page numbers and tokens.
178
+
179
+
- `hasToken` - `pageNumber: number => bool`
180
+
181
+
A function which can be invoked with a page number to check if there is a pagination token for that page. Useful to conditionally enable pagination buttons (seeexamples).
182
+
183
+
**Uncontrolled only**
184
+
185
+
- `result.pageNumber` - `number`
186
+
187
+
The current page number.
188
+
189
+
- `result.pageSize` - `number`
190
+
191
+
The current page size.
192
+
193
+
- `result.changePageNumber(changer)`
194
+
195
+
A function to change the page number. Changer is either a number, which will be the new page number, or a function, which gets the current page number as its first argument and returns the new page number.
A function to change the page size. Changer is either a number, which will be the new page size, or a function, which gets the current page size as its first argument and returns the new page size.
The Hook provides two ways to update the mapping between a page number and the token used to paginate from the current page to the next: a declarative one based on a React Hook and an imperative one based on a plain function.
217
+
218
+
#### Declarative
219
+
220
+
The declarative approach is based on React Hooks and it's useful when you're invoking an API via a React Hook, as when using [`axios-hooks`](https://github.com/simoneb/axios-hooks/), [`graphql-hooks`](https://github.com/nearform/graphql-hooks) or one of the many other Hook-based libraries available.
221
+
222
+
```jsx
223
+
const { useUpdateToken } =useTokenPagination(...)
224
+
225
+
// invoke your API which returns the token for the next page, e.g.
226
+
const { data, nextPage } =useYourApi()
227
+
228
+
// update the token for the next page using the Hook
229
+
useUpdateToken(nextPage)
230
+
```
231
+
232
+
#### Imperative
233
+
234
+
The imperative approach is useful when you invoke your API imperatively, for instance using `fetch` in a `useEffect` Hook:
0 commit comments