Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3354

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T01:56:08+00:00 2024-11-26T01:56:08+00:00

Building Pagination in React with React Paginate

  • 61k

Introduction

Pagination is a crucial component in any web application, as it allows for the efficient and seamless navigation of large amounts of data. In React, implementing pagination can be a challenge for developers, especially for those who are new to the framework. However, with the right understanding of React components and hooks, building pagination in React can be a straightforward process.

In this article, we will explore the steps involved in building a pagination system in React, from setting up the component structure to integrating it with your application's data. This guide will provide a comprehensive overview of the process and provide you with the knowledge and skills you need to build an effective and user-friendly pagination system.

We are going to look into the following topics in the article:

  • Installing Component
  • Understanding the react-paginate library with props
  • Data filtering for different page
  • Pagination
  • Conclusion

Now, let's get started.

Installing Library

We are going to use the react-paginate library to build our pagination features. You can install the library with the below command:

npm i react-paginate 
Enter fullscreen mode Exit fullscreen mode

Along with that, we will need some icons. For icons, I am using the react-icons library. Install it with the below command:

npm install react-icons --save 
Enter fullscreen mode Exit fullscreen mode

App.js

We are building an application that is going to have data in an array. We need to display only 3 data on each page(We can change as per requirement).

Let's start building them one by one.

Imports

Here are the imports that we will require for the component.

import ReactPaginate from "react-paginate"; // for pagination import { AiFillLeftCircle, AiFillRightCircle } from "react-icons/ai"; // icons form react-icons import { IconContext } from "react-icons"; // for customizing icons import { useEffect, useState } from "react"; // useState for storing data and useEffect for changing data on click  import "./styles.css"; // stylesheet 
Enter fullscreen mode Exit fullscreen mode

You can look for the comments for each import.

Data

The data is simple, it's the letter of the alphabet in an array.

const data = [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ]; 
Enter fullscreen mode Exit fullscreen mode

useEffect and useState

Here are the states of our application.

const [page, setPage] = useState(0); const [filterData, setFilterData] = useState(); const n = 3 
Enter fullscreen mode Exit fullscreen mode

page: It is for storing the current page number for the pagination component

filterData: It is the data that will be shown after filtering data for each page.

n: It is the maximum number of items to show on a page.

Here is the code for the useEffect hook that we are using to filter data. The data will filter when the user change to a different page. On each page, there will be filtered data for displaying.

useEffect(() => {   setFilterData(     data.filter((item, index) => {       return (index >= page * n) & (index < (page + 1) * n);     })   ); }, [page]); 
Enter fullscreen mode Exit fullscreen mode

The return formula will return the item from the data array in an array with an index in the multiple of 3.

Return

In the return statement, at the top, we are displaying the filterData.

<ul> {filterData && filterData.map((item, index) => <li>Item #{item}</li>)} </ul>; 
Enter fullscreen mode Exit fullscreen mode

Now, let's look into the ReactPaginate component. First, let's look at the code then I will explain each prop.

<ReactPaginate   containerClassName={"pagination"}   pageClassName={"page-item"}   activeClassName={"active"}   onPageChange={(event) => setPage(event.selected)}   pageCount={Math.ceil(data.length / n)}   breakLabel="..."   previousLabel={     <IconContext.Provider value={{ color: "#B8C1CC", size: "36px" }}>       <AiFillLeftCircle />     </IconContext.Provider>   }   nextLabel={     <IconContext.Provider value={{ color: "#B8C1CC", size: "36px" }}>       <AiFillRightCircle />     </IconContext.Provider>   } />; 
Enter fullscreen mode Exit fullscreen mode

Props Description
containerClassName You can provide a class name for the container of the pagination component.
pageClassName It is for the class name for each page number.
activeClassName Classname of the active page.
onPageChange It is the function that will be triggered on changing page. In this, we are storing the page number in the page state. Also, it starts with 0 and the number in the component starts with 1.
pageCount It is a required prop. As the name suggests, it will display the number of pages. I have divided the length of the data by the number of each item on a page.
breakLabel The component will be displayed when there will be a break between pages. It is to show that there is more page in between.
previousLabel The icon component is used here for displaying the previous button.
nextLable Here it is for the next button.

CSS

Here is the CSS of each component used in the code.

.App {   display: flex;   font-family: sans-serif;   margin: 0 auto;   align-items: center;   justify-content: center;   flex-direction: column; }  .pagination {   list-style: none;   height: 31.5px;   width: 31.5px;   display: flex;   justify-content: center;   align-items: center;   margin-top: 2px;   cursor: pointer; }  .active {   background-color: #1e50ff;   border-radius: 50%; }  .page-item {   list-style: none;   padding: 2px 12px;   height: 31.5px;   width: 31.5px;   display: flex;   justify-content: center;   align-items: center;   margin-top: 2px; } 
Enter fullscreen mode Exit fullscreen mode

CodeSandbox

You can look into the complete code and the output of the project in the below codesandbox.

Conclusion

Building pagination in React is an important aspect of web development and can greatly enhance the user experience in navigating large amounts of data. By following the steps outlined in this article, you can easily implement pagination in your React application and provide your users with a smooth and efficient way to access the data they need.

I hope the articles have helped you in understanding the process of creating a pagination component in React. Thanks for reading the article.

beginnersjavascriptreactwebdev
  • 0 0 Answers
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.