How to Customize Pagination in Next.js ?
Last Updated :
25 Jul, 2024
In this article, we will learn How we can add customized pagination in the NextJS project using Algolia. NextJS is a React-based framework. It has the power to Develop beautiful Web applications for different platforms like Windows, Linux, and mac. The linking of dynamic paths helps in rendering your NextJS components conditionally.
Approach: To add our customized pagination first we are going to create an account in algolia that enables us to search content in milliseconds. After that, we will get the API keys that we will use later in our app. Then we will create a new index to upload our data. On the homepage of our app, we will fetch the data from algolia using the API keys and algoliasearch module. Then we will create our customized pagination.
Create NextJS Application:
Step 1: You can create a new NextJs project using the below command:
npx create-next-app gfg
Step 2: To add Algolia search in our project we are going to install two modules:
npm install algoliasearch react-instantsearch-dom @material-ui/core @material-ui/lab
Project Structure: It will look like the following.

Step 3: Setting up Algolia. Algolia enables developers to build next-generation apps with APIs that deliver relevant content in milliseconds. So to use algolia first create a free account and get the API keys of that account.
1. To get the API keys Go to settings > API Keys

2. After that create an index and upload the data that you want to search. You can upload the data in json, csv format or by using their API.

For this example, I am uploading the below data.
Title, Tag, Day
GFG1, python, Monday
GFG2, java, Tuesday
GFG3, css, Wednesday
GFG4, html, Thursday
GFG5, react, Friday
GFG6, nextjs, Saturday

Step 4: Now we will create custom pagination for our app. Â For this, we will create a new file inside a new components folder with the below content.Â
JavaScript
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Pagination from '@material-ui/lab/Pagination';
import { connectPagination } from 'react-instantsearch-dom';
const useStyles = makeStyles((theme) => ({
root: {
'& > *': {
marginTop: theme.spacing(2),
},
},
}))
function Pagin({ currentRefinement,
nbPages, refine, createURL }) {
const classes = useStyles();
const handleChange = (event, value) => {
event.preventDefault();
refine(value);
};
return (
<div className={classes.root} >
<Pagination count={nbPages} shape="rounded"
onChange={handleChange} />
</div>
);
}
export default connectPagination(Pagin);
Step 5: Now we can use the API to add the customized pagination in NextJs Project. Â After that to use our customized pagination we are going to add the below code in the index.js file.
JavaScript
// Importing modules
import algoliasearch from "algoliasearch/lite";
import { InstantSearch, Hits, Configure }
from "react-instantsearch-dom";
import Pagination from "../components/CustomPagination";
const searchClient = algoliasearch(
APPLICATION_API_KEY,
SEARCH_ONLY_API_KEY,
);
export default function CustomizedFilter() {
return (
<>
<InstantSearch
searchClient={searchClient}
indexName="gfg_dev">
<Configure hitsPerPage={3}/>
{/* Adding Data */}
<Hits />
{/* Adding Pagination */}
<Pagination totalPages={5}/>
</InstantSearch>
</>
);
}
You can also add styling using CSS in our customized Pagination.
Steps to run the application: Run the app using the below command in the terminal.
npm run dev
Output:
Similar Reads
How to customize search bar in Next.js
Customizing a search bar in Next.js enhances user experience by providing fast, efficient content search. This guide covers setting up Algolia, fetching data, and integrating a custom search bar.In this article, we will learn How we can add a customized search bar in the NextJS project using Algolia
3 min read
How to customize filters in Next.js ?
In this article, we will learn How we can add customized filters in the NextJS project using Algolia. NextJS is a React-based framework. It has the power to Develop beautiful Web applications for different platforms like Windows, Linux, and mac. The linking of dynamic paths helps in rendering your N
3 min read
How to customize links for pagination in Bootstrap ?
In many websites, we notice that when we search for something then all the related content is shown but if the number of content is large then it will make the website longer. To solve this, there is pagination in the webpage i.e, the contents are divided into many pages and on clicking the user ca
4 min read
How to create a Custom Error Page in Next.js ?
Creating a custom error page in Next.js allows you to provide a better user experience by customizing the appearance and messaging of error pages like 404 and 500.In this post we are going to create a custom error page or a custom 404 page in a Next JS website.What is a custom error page?The 404 pag
2 min read
Laravel Pagination Customizations
To learn about Customize Pagination, you want to understand the following points. What is paginationTypes of paginationWhat are the methodsSteps to write customize laravel  paginationWhat is pagination? Pagination is a navigation bar that helps users to move from one page to another page. Customize
5 min read
How to add Pagination in Nextjs using Algolia ?
Adding pagination to a Next.js application with Algolia involves fetching and displaying paginated search results from Algoliaâs API. This setup provides an efficient way to handle large datasets by loading data in chunks. In this article, we will learn How we can add pagination in the NextJS projec
2 min read
How to Get URL pathname in Next.js?
Next.js is a powerful React framework that simplifies the process of building server-rendered and statically generated web applications. One common requirement in web development is to get the URL pathname, which can be useful for various purposes such as conditional rendering, routing, and analytic
3 min read
How To Get Current Route In Next.js?
Next.js is a popular React framework that makes it easy to build server-side rendered and static web applications. One common task in web development is determining the current route or URL of the page. In Next.js, this can be done using the built-in useRouter hook. This article will guide you throu
3 min read
How to do pagination Node.js with MySQL ?
Node.js is a runtime environment like Chrome's V8 JavaScript engine. Node.js is an open-source, cross-platform, and backend runtime environment that executes outside a web browser.MySQL is an open-source relational database management system that is fast, reliable, flexible, and robust. Both MySQL a
8 min read
How to Create Pagination in Node.js using Skip and Limit ?
Creating pagination in Node.js using the skip and limit methods. This approach efficiently retrieves specific data subsets from a database, improving performance and user experience by loading content in manageable segments rather than all at once. What is Pagination?Pagination is a very helpful met
4 min read