How To Create A Custom MUI Search Bar Component In NextJS?
Last Updated :
01 Jul, 2024
Creating a custom Material-UI (MUI) search bar in the NextJS application is a great way to enhance the user experience by providing seamless and stylish search functionality. In this article, we will guide you through the steps to create a custom MUI search bar component in a Next.js project.
Prerequisites
There are various ways to create a search bar in a Next.js application using Material-UI. Here are two possible approaches
Approach 1: Using Material-UI Text Field with State Management
This approach involves using the Material-UI Text Field component and managing the search input state within the component.
Syntax:
import React, { useState } from 'react';
import { TextField } from '@mui/material';
const SearchBar = () => {
const [searchTerm, setSearchTerm] = useState('');
const handleSearch = (event) => {
setSearchTerm(event.target.value);
};
return (
<TextField
label="Search"
variant="outlined"
value={searchTerm}
onChange={handleSearch}
fullWidth
/>
);
};
export default SearchBar;
Approach 2: Using Material-UI Autocomplete Component
This approach uses the Autocomplete component from Material-UI for an enhanced search bar with suggestions.
import React, { useState } from 'react';
import { Autocomplete, TextField } from '@mui/material';
const SearchBar = ({ options }) => {
const [searchTerm, setSearchTerm] = useState('');
const handleSearch = (event, value) => {
setSearchTerm(value);
};
return (
<Autocomplete
freeSolo
options={options}
value={searchTerm}
onInputChange={handleSearch}
renderInput={(params) => (
<TextField
{...params}
label="Search"
variant="outlined"
fullWidth
/>
)}
/>
);
};
export default SearchBar;
Steps to Create Application
Step 1: Create a Next.js Application
First, create a new Next.js application if you haven't already:
npx create-next-app my-search-bar-app
cd my-search-bar-app
Step 2: Install Material-UI
Next, install Material-UI and its dependencies:
npm install @mui/material @emotion/react @emotion/styled
Folder Structure:
Folder StructureUpdated Dependencies:
{
"dependencies":
{ "@emotion/react": "^11.7.1",
"@emotion/styled": "^11.7.1",
"@mui/material": "^5.2.0",
"next": "12.0.7",
"react": "17.0.2",
"react-dom": "17.0.2"
}
}
Example: To demonstrate creating a custom MUI search bar component in NextJs.
JavaScript
// pages/index.js:
import Head from 'next/head';
import SearchBar from '/pages/components/SearchBar';
export default function Home() {
const options = ['Option 1', 'Option 2', 'Option 3'];
return (
<div>
<Head>
<title>Custom MUI Search Bar</title>
</Head>
<main>
<h1>Custom MUI Search Bar</h1>
<SearchBar options={options} />
</main>
</div>
);
}
JavaScript
// components/SearchBar.js:
import React, { useState } from 'react';
import { Autocomplete, TextField } from '@mui/material';
const SearchBar = ({ options }) => {
const [searchTerm, setSearchTerm] = useState('');
const handleSearch = (event, value) => {
setSearchTerm(value);
};
return (
<Autocomplete
freeSolo
options={options}
value={searchTerm}
onInputChange={handleSearch}
renderInput={(params) => (
<TextField
{...params}
label="Search"
variant="outlined"
fullWidth
/>
)}
/>
);
};
export default SearchBar;
Output
The output will be a responsive search bar that allows users to input text and search through the provided options. Below is a screenshot of the expected output:
How To Create A Custom MUI Search Bar Component In NextJS
Similar Reads
How To Create Dynamic Breadcrumbs Component in NextJS?
Breadcrumbs are an essential navigational aid in web applications, especially in multi-level hierarchies. In a Next.js application, creating a dynamic breadcrumbs component can enhance the user experience by providing clear navigation paths. In this guide, we'll walk through the process of building
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 Algolia
3 min read
How to Create Autosuggest Component using MUI and NextJS?
Autosuggest components are essential for enhancing user experience by providing real-time suggestions as users type. This aspect is especially important for searches in online shopping and when searching for favourite songs or movies on platforms like YouTube and Netflix. In these contexts, autosugg
4 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 create dynamic search box in ReactJS?
The dynamic search box is a search bar with a text field to take the user input and then perform some operation on the user input to show him the dynamic results based on his input. API calls are made whenever a user starts typing in order to show him the dynamic options. For example Youtube search
2 min read
How To Create A Custom Image Loader In NextJS?
Creating a custom image loader in Next.js allows you to control how images are loaded and optimized. This is useful for integrating with different image services or handling specific image processing needs. Hereâs how to create and use a custom image loader: This article will walk you through the st
4 min read
How to Add Auto-Complete Search Box in ReactJS?
An autocomplete search box is a user interface feature that offers suggestions as users type their queries. It enhances the user experience by providing real-time suggestions and help users find what they're looking for more quickly. In this article, we will explore how to implement an autocomplete
5 min read
Create a Custom Hook to Make Next.js Apps Responsive
Creating a custom hook for responsiveness in Next.js enables you to handle various screen sizes and device orientations effectively. This approach helps maintain a consistent user experience across different devices by encapsulating responsive logic into a reusable and manageable component.ApproachT
3 min read
How to Create a Sidebar in NextJS & Tailwind CSS?
In web development, sidebars are commonly used to organize navigation elements and provide quick access to different sections of a website. A sidebar is a vertical menu or panel that is typically positioned on the left or right side of the main content area. Prerequisites:Next.jsTailwindJavaScriptSt
5 min read
How to add Draggable Components in Next.js ?
In this article, we are going to learn how we can add Draggable Components 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
2 min read