Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement pagination in React

I am new to ReactJS and am creating a simple TODO application in it. Actually, it is a very basic app with no DB connection, tasks are stored in an array. I added Edit and Delete functionality to it now I want to add pagination.

How do I implement it? Any help will be appreciated. Thank you...!!

like image 540
Nitesh Avatar asked Oct 25 '16 06:10

Nitesh


People also ask

How do I apply pagination in ReactJS?

Fetching the Data jsx file. This will hold all your components as well as the logic for fetching the data and dividing it for pagination. Now, add the following two states to the App component. const [data, setData] = useState([])const [loading, setLoading] = useState(true);

How is pagination implemented?

For example, you can implement pagination using links to new pages on your ecommerce site, or using JavaScript to update the current page. Load more and infinite scroll are generally implemented using JavaScript.

Why we use pagination in react JS?

Pagination in React JS is a function of apps that allows them to show data on a series of pages. The information can be seen by going from one page to the next rather than viewing it. This process is frequently done in conjunction with server-side technology that allows clients to make group requests for data.


1 Answers

I've implemented pagination in pure React JS recently. Here is a working demo: http://codepen.io/PiotrBerebecki/pen/pEYPbY

You would of course have to adjust the logic and the way page numbers are displayed so that it meets your requirements.

Full code:

class TodoApp extends React.Component {   constructor() {     super();     this.state = {       todos: ['a','b','c','d','e','f','g','h','i','j','k'],       currentPage: 1,       todosPerPage: 3     };     this.handleClick = this.handleClick.bind(this);   }    handleClick(event) {     this.setState({       currentPage: Number(event.target.id)     });   }    render() {     const { todos, currentPage, todosPerPage } = this.state;      // Logic for displaying todos     const indexOfLastTodo = currentPage * todosPerPage;     const indexOfFirstTodo = indexOfLastTodo - todosPerPage;     const currentTodos = todos.slice(indexOfFirstTodo, indexOfLastTodo);      const renderTodos = currentTodos.map((todo, index) => {       return <li key={index}>{todo}</li>;     });      // Logic for displaying page numbers     const pageNumbers = [];     for (let i = 1; i <= Math.ceil(todos.length / todosPerPage); i++) {       pageNumbers.push(i);     }      const renderPageNumbers = pageNumbers.map(number => {       return (         <li           key={number}           id={number}           onClick={this.handleClick}         >           {number}         </li>       );     });      return (       <div>         <ul>           {renderTodos}         </ul>         <ul id="page-numbers">           {renderPageNumbers}         </ul>       </div>     );   } }   ReactDOM.render(   <TodoApp />,   document.getElementById('app') ); 
like image 90
Piotr Berebecki Avatar answered Oct 02 '22 18:10

Piotr Berebecki