A React Pagination component, which is lightweight, fast and easy to use.
Live Demo
https://serhat-m.github.io/pagination-react-js
Install
npm i pagination-react-js
Example
import { generateTestData, usePagination, Pagination } from "pagination-react-js"
const App = () => {
const { currentPage, entriesPerPage, entries } = usePagination(1, 10)
const dataList = generateTestData(700, (i) => ({ id: `Id${i}`, name: `Name${i}` }))
return (
<div className="container">
<table>
<tbody>
{
dataList.slice(entries.indexOfFirst, entries.indexOfLast).map(entry => {
return <tr key={entry.id}>
<td>{entry.id}</td>
<td>{entry.name}</td>
</tr>
})
}
</tbody>
</table>
<Pagination
entriesPerPage={entriesPerPage.get}
totalEntries={dataList.length}
currentPage={{ get: currentPage.get, set: currentPage.set }}
offset={3}
classNames={{
wrapper: "pagination m-auto",
item: "pagination-item",
itemActive: "pagination-item-active",
navPrev: "pagination-item nav-item",
navNext: "pagination-item nav-item",
navStart: "pagination-item nav-item",
navEnd: "pagination-item nav-item",
navPrevCustom: "pagination-item",
navNextCustom: "pagination-item"
}}
showFirstNumberAlways={true}
showLastNumberAlways={true}
navStart="«" // Here you can pass anything (Text, HTML Tag, React Component, ...)
navEnd="»" // Here you can pass anything (Text, HTML Tag, React Component, ...)
navPrev="‹" // Here you can pass anything (Text, HTML Tag, React Component, ...)
navNext="›" // Here you can pass anything (Text, HTML Tag, React Component, ...)
navPrevCustom={{ steps: 5, content: "\u00B7\u00B7\u00B7" /* Here you can pass anything (Text, HTML Tag, React Component, ...) */ }}
navNextCustom={{ steps: 5, content: "\u00B7\u00B7\u00B7" /* Here you can pass anything (Text, HTML Tag, React Component, ...) */ }}
/>
</div>
)
}
export default App
API
Used to generate pagination relevant data and state.
Function Parameters
initialPage
number
Initial active page numbermaxEntriesPerPage
number
Maximum number of entries per page
Returns
{
currentPage: {
get: number,
set: (arg: number) => void
},
entriesPerPage: {
get: number,
set: (arg: number) => void
},
entries: {
indexOfFirst: number,
indexOfLast: number
}
}
Used to generate the pagination.
Props
entriesPerPage: entriesPerPage.get
totalEntries: yourDataList.length
currentPage: {{ get: currentPage.get, set: currentPage.set }}
offset: number
classNames?: {
wrapper?: string,
item?: string,
itemActive?: string,
navStart?: string,
navEnd?: string,
navPrev?: string,
navNext?: string,
navPrevCustom?: string,
navNextCustom?: string
}
showFirstNumberAlways?: true
showLastNumberAlways?: true
navStart?: any
navEnd?: any
navPrev?: any
navNext?: any
navPrevCustom?: { steps: number, content: any }
navNextCustom?: { steps: number, content: any }
Example rendered structure
<ul class="pagination">
<li class="pagination-item nav-item">Go to the start</li>
<li class="pagination-item nav-item">Step backwards</li>
<li class="pagination-item">1</li>
<li class="pagination-item">2</li>
<li class="pagination-item pagination-item-active">3</li>
<li class="pagination-item">4</li>
<li class="pagination-item">5</li>
<li class="pagination-item nav-item">Step forward</li>
<li class="pagination-item nav-item">Go to the end</li>
</ul>
Example CSS
body {
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", sans-serif;
}
.pagination {
display: flex;
flex-direction: row;
justify-content: center;
gap: 7px;
width: fit-content;
list-style-type: unset;
border: 2px solid #e1e4e7;
padding: 8px 10px;
border-radius: 5px;
-webkit-touch-callout: none;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
.pagination-item {
width: 30px;
height: 30px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 5px;
}
.pagination-item:hover {
cursor: pointer;
background-color: #e1e4e7;
-webkit-transition: background-color .1s linear;
-moz-transition: background-color .1s linear;
-o-transition: background-color .1s linear;
transition: background-color .1s linear;
}
.pagination-item-active {
color: white;
background-color: #0a7ea3;
pointer-events: none;
-webkit-transition: background-color .1s linear;
-moz-transition: background-color .1s linear;
-o-transition: background-color .1s linear;
transition: background-color .1s linear;
}
.pagination-item-active:hover {
background-color: #0a7ea3;
}
Function: generateTestData(numberOfEntries, cb)
Used to generate test data.
Function Parameters
numberOfEntries
number
Number of entriescb
function
Callback that receives an incrementing number as a parameter
Returns
Array with test data.
Example
import { generateTestData } from "pagination-react-js"
const dataList = generateTestData(700, i => ({ id: i, name: `Test ${i}` }))