How to customize filters in Next.js ?
Last Updated :
01 Aug, 2024
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 NextJS components conditionally.
Approach: To add our customized filters 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 filters.
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
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 filters for our app. Â For this, we will create a new file inside a new components folder with the below content.Â
JavaScript
import { connectMenu } from 'react-instantsearch-dom';
const Filters = ({ items, currentRefinement, refine }) => (
<select
value={currentRefinement || ''}
onChange={event => refine(event.currentTarget.value)}
style={{padding:'10px 100px 10px 100px',borderRadius:'20px'}}
>
<option value="">See all options</option>
{items.map(item => (
<option
key={item.label}
value={item.isRefined ? currentRefinement : item.value}
>
{item.label}
</option>
))}
</select>
);
export default connectMenu(Filters);
Step 5: Now we can use the API to add the customized filter in NextJs Project. Â After that to use our customized filter we are going to add the below code in the index.js file.
JavaScript
// Importing modules
import algoliasearch from "algoliasearch/lite";
import { InstantSearch, Hits } from "react-instantsearch-dom";
import Filter from "../components/CustomFilter";
const searchClient = algoliasearch(
APPLICATION_API_KEY,
SEARCH_ONLY_API_KEY,
);
export default function CustomizedFilter() {
return (
<>
<InstantSearch
searchClient={searchClient}
indexName="gfg_dev">
{/* Adding Filter */}
<Filter attribute="Title"/>
{/* Adding Data */}
<Hits />
</InstantSearch>
</>
);
}
You can also add styling using CSS in our customized filter.
Steps to run the application: Run the app using the below command in the terminal.
npm run dev
Output:
Similar Reads
How to Add Custom Local Fonts in Next.js ?
Adding custom fonts in Next.js project enhances the typography. It also improves the performance as the fonts are loaded directly from project assets. They can be integrated by importing font files into a project, defining @font-face rules in CSS, and applying them across components for consistent s
2 min read
How to Customize Pagination in Next.js ?
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 you
3 min read
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 Algoli
3 min read
How to add Filters in Next.js using Algolia ?
In this article, we will learn How we can add 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 NextJS compo
4 min read
How to add File Dropper in Next.js ?
Adding a file dropper feature in Next.js involves setting up a drag-and-drop interface, handling file uploads on the frontend, and processing the files on the server side. In this article, we are going to learn how we can add File Dropper in NextJS. ApproachTo add our File Dropper we are going to us
2 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 p
2 min read
How to Load Data from a File in Next.js?
Loading Data from files consists of using client-side techniques to read and process files in a Next.js application. In this article, we will explore a practical demonstration of Load Data from a File in Next.js. We will load a validated CSV input file and display its contents in tabular format. App
3 min read
How to add Calendar in Next.js ?
Adding a calendar to a Next.js application enhances scheduling and event management. We can just install and use the available npm package. In this article, we are going to learn how we can add a calendar loader in NextJS. ApproachTo add our calendar we are going to use the react-calendar package. T
2 min read
How to add custom filter in search Box in ReactJS?
The custom filter means adding our own business login to filter the options shown in searchbox or any dropdown menu. Material UI for React has this component available for us and it is very easy to integrate. We can create our own custom filter for options in ReactJS using the following approach. Cr
2 min read
How to add CodeBlock in Next.js ?
In this article, we are going to learn how we can add CodeBlock in NextJS. 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 conditiona
2 min read