11React Pagination
22=============
33
4- This package includes pagination hooks for React. The hooks support saving the search and pagination values in local state or in the browser URL.
4+ This package includes pagination hooks for React. The hooks support saving the search and pagination values in local
5+ state or in the browser URL.
56
67## Table of content
78
89- [ Usage] ( #usage )
9- - [ useSearch] ( #usesearch )
10- - [ usePagination] ( #usepagination )
11- - [ useSearchAndPagination] ( #usesearchandpagination )
12- - [ Supported Implementations] ( #supported-implementations )
13- - [ In Memory Pagination] ( #in-memory-pagination )
14- - [ React-Router based pagination] ( #router-based-pagination )
15- - [ NextJS Router based pagination] ( #nextjs-router-based-pagination )
10+ - [ useSearchAndPagination] ( #usesearchandpagination )
11+ - [ Supported Implementations] ( #supported-implementations )
12+ - [ In Memory Pagination] ( #in-memory-pagination )
13+ - [ React-Router based pagination] ( #react-router-based-pagination )
14+ - [ NextJS Router based pagination] ( #nextjs-router-based-pagination )
1615- [ Build & Publish] ( #build--publish )
1716- [ Information] ( #information )
1817
@@ -24,120 +23,68 @@ First, you have to install the package:
2423npm install @aboutbits/react-pagination
2524```
2625
27- Second, you can make use of the different hooks.
26+ Second, you can make use of the ` useSearchAndPagination ` hook. This package implements 3 versions of this hook:
2827
29- - [ useSearch] ( #usesearch )
30- - [ usePagination] ( #usepagination )
31- - [ useSearchAndPagination] ( #usesearchandpagination )
28+ - [ In Memory] ( #in-memory-pagination ) : Use this hook where you don't want to modify browser history. e.g. Dialogs
29+ - [ React Router] ( #react-router-based-pagination ) : Use this hook if you want to keep track of the state in the URL and
30+ your project is using React Router.
31+ - [ NextJS Router] ( #nextjs-router-based-pagination ) : Use this hook if you want to keep track of the state in the URL and
32+ your project is using NextJS.
3233
33- ### useSearch
34-
35- This hook saves the value of your search input and lets you change it or clear it.
34+ ### useSearchAndPagination
3635
37- ``` tsx
36+ This hook supports the combination of a search value and pagination and manages the state of the search value, and the
37+ pagination values.
3838
39- import { useSearch } from ' @aboutbits/react-pagination/nextRouterPagination '
39+ The hook returns the following object:
4040
41- const users = [
42- ' Alex' , ' Nadia' , ' Natan' , ' Marie' , ' Moritz'
43- ]
44-
45- function UserList() {
46- const { search, searchActions } = useSearch ()
47-
48- return (
49- <div >
50- <input onChange = { searchActions .search } />
51-
52- <ul >
53- { users
54- .filter (user => user .startsWith (search ))
55- .map (user => <li >{ user } </li >)}
56- </ul >
57- </div >
58- )
59- }
60- ```
61-
62- ### usePagination
63-
64- This hook saves the value of your current page and lets you change it or clear it.
41+ | value| type| description|
42+ | ---| ---| ---|
43+ | search| string| value of your search parameter|
44+ | page| number| value of the current page|
45+ | size| number| max elements in a single page|
46+ | actions| object| object with 3 functions: search, setPage, clear|
6547
6648``` tsx
67-
68- import { usePagination } from ' @aboutbits/react-pagination/nextRouterPagination"
49+ import { useSearchAndPagination } from ' @aboutbits/react-pagination/nextRouterPagination'
6950
7051const users = [
71- [' Alex' , ' Nadia' ],
72- [' Natan' , ' Marie' ],
73- [' Moritz' , ' Franz' ]
52+ ' Alex' , ' Simon' , ' Natan' , ' Nadia' , ' Moritz' , ' Marie'
7453]
7554
7655function UserList() {
77- const { page, size, paginationActions } = usePagination ()
78-
56+ const { page, size, search, actions } = useSearchAndPagination ()
57+
7958 return (
8059 <div >
81- <select onSelect = { paginationActions .setPage } >
82- <option value = { 0 } >First Page</option >
83- <option value = { 1 } >Second Page</option >
84- <option value = { 2 } >Third Page</option >
85- </select >
86-
87- <ul >
88- { users [page ]
89- .map (user => <li >{ user } </li >)}
90- </ul >
91- </div >
92- )
93- }
94- ```
95-
96- ### useSearchAndPagination
97-
98- This hook supports the combination of a search value and pagination.
99- The ` clear ` function clears the search value and resets the page to the first page.
100-
101- ``` tsx
102- import { useSearchAndPagination } from ' @aboutbits/react-pagination/nextRouterPagination'
103-
104- const users = [
105- ' Alex' , ' Simon' , ' Natan' , ' Nadia' , ' Moritz' , ' Marie'
106- ]
107-
108- function UserList() {
109- const { page, size, search, searchActions, paginationActions } = useSearchAndPagination ()
110-
111- return (
112- <div >
113- <input onChange = { searchActions .search } />
114- <button onClick = { searchActions .clear } >Clear Input</button >
115- <select onSelect = { paginationActions .setPage } >
116- <option value = { 0 } >First Page</option >
117- <option value = { 1 } >Second Page</option >
60+ <input onChange = { (value ) => actions .search (value )} />
61+ <button onClick = { () => actions .clear ()} >Clear Input</button >
62+ <select onSelect = { (value ) => actions .setPage (value )} >
63+ <option value = { 0 } >First Page</option >
64+ <option value = { 1 } >Second Page</option >
11865 </select >
119-
66+
12067 <ul >
121- { users .filter (user => user .startsWith (search ))
122- .slice (page , page + size )
123- .map (user => <li >{ user } </li >)}
68+ { users .filter (user => user .startsWith (search ))
69+ .slice (page , page + size )
70+ .map (user => <li >{ user } </li >)}
12471 </ul >
125- </div >
126- )
72+ </div >
73+ )
12774}
12875```
12976
13077## Supported implementations
13178
132- This package includes 3 different implementations of the above hooks.
79+ This package includes 3 different implementations of the above hook.
80+
13381- [ In Memory] ( #in-memory-pagination )
13482- [ React Router] ( #react-router-based-pagination )
13583- [ NextJS Router] ( #nextjs-router-based-pagination )
13684
13785### In Memory Pagination
13886
139- Use this pagination hook if you want to keep track of the pagination in memory.
140- This is very handy for dialogs.
87+ Use this pagination hook if you want to keep track of the pagination in memory. This is very handy for dialogs.
14188
14289``` tsx
14390import { useSearchAndPagination } from ' @aboutbits/react-pagination/inMemoryPagination'
@@ -153,7 +100,8 @@ import { useSearchAndPagination } from '@aboutbits/react-pagination/reactRouterP
153100
154101### NextJS Router based pagination
155102
156- These are specific hooks for applications that use [ NextJS Router] ( https://nextjs.org/docs/api-reference/next/router ) for routing.
103+ These are specific hooks for applications that use [ NextJS Router] ( https://nextjs.org/docs/api-reference/next/router )
104+ for routing.
157105
158106``` tsx
159107import { useSearchAndPagination } from ' @aboutbits/react-pagination/nextRouterPagination'
@@ -171,16 +119,15 @@ npm version major
171119
172120## Information
173121
174- About Bits is a company based in South Tyrol, Italy. You can find more information about us on [ our website] ( https://aboutbits.it ) .
122+ About Bits is a company based in South Tyrol, Italy. You can find more information about us
123+ on [ our website] ( https://aboutbits.it ) .
175124
176125### Support
177126
178127For support, please contact [ info@aboutbits.it ] ( mailto:info@aboutbits.it ) .
179128
180129### Credits
181130
182- - [ Martin Malfertheiner] ( https://github.com/mmalfertheiner )
183- - [ Alex Lanz] ( https://github.com/alexlanz )
184131- [ All Contributors] ( ../../contributors )
185132
186133### License
0 commit comments