During development, we were facing problems supporting server-rendering of our web app & SEO (require pagination links). To solve that, we had to add 2 snippets of code, one to support the server-side and another to support the client-side which lead to being hard for maintenance.
During development, we were facing problems supporting server-rendering of our web app & SEO (require pagination links). To solve that, we had to add 2 snippets of code, one to support the server-side and another to support the client-side which lead to being hard for maintenance. Most of the pagination libraries only support client-rendering by attaching event handlers on the pagination button to redirect. Because of that, we created this library which allows flexibly to customize behaviors (attaching event handlers or href attribute) and user interface.
The component applied Render Props
pattern. (You can read more about this pattern here).
This approach allows you to fully control UI component and behaviours.
npm install --save react-paginating
or
yarn add react-paginating
You can check out the basic demo here:
.bg-red {
background-color: red;
}
import React from 'react';
import { render } from 'react-dom';
import Pagination from 'react-paginating';
const fruits = [
['apple', 'orange'],
['banana', 'avocado'],
['coconut', 'blueberry'],
['payaya', 'peach'],
['pear', 'plum']
];
const limit = 2;
const pageCount = 3;
const total = fruits.length * limit;
class App extends React.Component {
constructor() {
super();
this.state = {
currentPage: 1
};
}
handlePageChange = (page, e) => {
this.setState({
currentPage: page
});
};
render() {
const { currentPage } = this.state;
return (
<div>
<ul>
{fruits[currentPage - 1].map(item => <li key={item}>{item}</li>)}
</ul>
<Pagination
className="bg-red"
total={total}
limit={limit}
pageCount={pageCount}
currentPage={currentPage}
>
{({
pages,
currentPage,
hasNextPage,
hasPreviousPage,
previousPage,
nextPage,
totalPages,
getPageItemProps
}) => (
<div>
<button
{...getPageItemProps({
pageValue: 1,
onPageChange: this.handlePageChange
})}
>
first
</button>
{hasPreviousPage && (
<button
{...getPageItemProps({
pageValue: previousPage,
onPageChange: this.handlePageChange
})}
>
{'<'}
</button>
)}
{pages.map(page => {
let activePage = null;
if (currentPage === page) {
activePage = { backgroundColor: '#fdce09' };
}
return (
<button
{...getPageItemProps({
pageValue: page,
key: page,
style: activePage,
onPageChange: this.handlePageChange
})}
>
{page}
</button>
);
})}
{hasNextPage && (
<button
{...getPageItemProps({
pageValue: nextPage,
onPageChange: this.handlePageChange
})}
>
{'>'}
</button>
)}
<button
{...getPageItemProps({
pageValue: totalPages,
onPageChange: this.handlePageChange
})}
>
last
</button>
</div>
)}
</Pagination>
</div>
);
}
}
render(<App />, document.getElementById('root'));
number
Total results
string
Customizable style for pagination wrapper
number
Number of results per page
number
How many pages number you want to display in pagination zone.
number
Current page number
function({ pageValue: number, onPageChange: func })
Allow to pass props and event to page item. When page is clicked, onPageChange
will be executed with param value pageValue
.
Note: This callback function should only use for paging with state change. If you prefer parsing page value from query
url (Please don't use this callback function).
array: [number]
List of pages number will be displayed. E.g: [1, 2, 3, 4, 5]
number
number
number
number
boolean
Check if it has next page
or not.
boolean
Check if it has previous page
or not.
If you don’t agree with the choices made in this project, you might want to explore alternatives with different tradeoffs. Some of the more popular and actively maintained ones are:
Author: ChoTotOSS
Source Code: https://github.com/ChoTotOSS/react-paginating
Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.
Increase Performance of React Applications Via Array JavaScript Methods. We will create a simple event management application in the react to add, update, and delete an event.
I have been using React JS in my projects for quite some time now and am used to managing routing in my app using the react-router package. I have always been keen on having as little dependencies in my apps as possible, so, I always felt perturbed by the use of this particular package in simpler apps which did not have complex routes.
In this post, I will share my own point of view about React Hooks, and as the title of this post implies, I am not a big fan.
This article will walk you through the concepts you would need to know to step into the world of widely used ReactJS.