Open In App

How to implement pagination in React Redux Applications?

Last Updated : 13 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Pagination is a common requirement in web applications. It is very helpful when we wish to display or manage a large dataset on the website in an aesthetic manner. Whenever it comes to displaying a large number of data, state handling comes into the picture. The idea of pagination has a good binding with redux. In this article, we will have a look at how to implement pagination using the redux state management library.

Approach to Implement Pagination in React Redux

In the code, we will make a fetch request to a fake REST API (JSON Placeholder) that serves todos. We will display 10 todos per page. On every page change, 10 more todos will be displayed. To implement this pagination with redux, we will set the initial state of the redux slice with pagination parameters. We will also create an async thunk to fetch todos from the API based on pagiantion state. Along the way, we will include the reducer to update the current page.

Steps to Create React App and Installing Modules

Step 1: Create a react app and enter into the directory created by running the following commands.

npx create-react-app redux-pagination
cd redux-pagination

Step 2: Install the required dependencies

npm install @reduxjs/toolkit react-redux redux-thunk

Project Structure:

red
project structure

Updated Dependencies: Open package.json to verify the following dependencies.

"dependencies": {
"@reduxjs/toolkit": "^2.2.1",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^9.1.0",
"react-scripts": "5.0.1",
"redux-thunk": "^3.1.0",
"web-vitals": "^2.1.4"
}

Example: Here is the working example of pagination using redux in react app.

CSS
/* src/App.css */

/* App.css */

.container {
    max-width: 600px;
    margin: 0 auto;
    padding: 20px;
}

.top-text {
    font-size: 20px;
    font-weight: 500;
    color: rgb(28, 158, 37);

}

.t-2-text {
    font-size: 12px;
    text-decoration: underline;
}

.fetch-button {
    padding: 10px 20px;
    margin-bottom: 20px;
    font-size: 16px;
    background-color: #f2176b;
    color: #fff;
    border: none;
    border-radius: 3px;
    cursor: pointer;
    transition: background-color 0.3s;
}

.fetch-button:hover {
    background-color: #bf2419;
}

.loading {
    margin-top: 20px;
    font-size: 60px;
}

.todos-list {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 12px;
    font-size: 8px;
}

.cards {
    padding: 8px;
    border-radius: 5px;
}

.top {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 10px;
}

.loader {
    width: 20px;
    --b: 3px;
    aspect-ratio: 1;
    border-radius: 50%;
    padding: 1px;
    background: conic-gradient(#0000 10%, #f03355) content-box;
    -webkit-mask:
        repeating-conic-gradient(#0000 0deg, #000 1deg 20deg, #0000 21deg 36deg),
        radial-gradient(farthest-side, #0000 calc(100% - var(--b) - 1px), #000 calc(100% - var(--b)));
    -webkit-mask-composite: destination-in;
    mask-composite: intersect;
    animation: l4 1s infinite steps(10);
}

@keyframes l4 {
    to {
        transform: rotate(1turn)
    }
}


.pagination {
    display: flex;
    gap: 24px;
    justify-content: center;
    align-items: center;
}

.pagination button {
    background-color: rgb(56, 122, 228);
    color: white;
    padding: 6px 12px;
    border-radius: 4px;
    border: none;
}
JavaScript JavaScript JavaScript JavaScript JavaScript

Output:

npm start

2024-03-1109-13-33-ezgifcom-video-to-gif-converter



Next Article

Similar Reads