How to create dynamic search box in ReactJS?
Last Updated :
12 Dec, 2022
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 box, Google Searchbox, etc. Material UI for React has this component available for us and it is very easy to integrate. We can create a simple dynamic search box in ReactJS using the following approach.
Creating React Application And Installing Module:
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Step 3: After creating the ReactJS application, Install the material-ui modules using the following command:
npm install @material-ui/core
npm install @material-ui/lab
Project Structure: It will look like the following.
Project StructureApp.js: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
JavaScript
import React, { useState } from 'react'
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';
const App = () => {
const [myOptions, setMyOptions] = useState([])
const getDataFromAPI = () => {
console.log("Options Fetched from API")
fetch('http://dummy.restapiexample.com/api/v1/employees').then((response) => {
return response.json()
}).then((res) => {
console.log(res.data)
for (var i = 0; i < res.data.length; i++) {
myOptions.push(res.data[i].employee_name)
}
setMyOptions(myOptions)
})
}
return (
<div style={{ marginLeft: '40%', marginTop: '60px' }}>
<h3>Greetings from GeeksforGeeks!</h3>
<Autocomplete
style={{ width: 500 }}
freeSolo
autoComplete
autoHighlight
options={myOptions}
renderInput={(params) => (
<TextField {...params}
onChange={getDataFromAPI}
variant="outlined"
label="Search Box"
/>
)}
/>
</div>
);
}
export default App
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

In the above example, you can see that whenever the user types anything, an API called is made to fetch the options, this is how we can show dynamic options in the search box just like YouTube, Google, etc.
Similar Reads
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
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 a Dictionary App in ReactJS ? In this article, we will be building a very simple Dictionary app with the help of an API. This is a perfect project for beginners as it will teach you how to fetch information from an API and display it and some basics of how React actually works. Also, we will learn about how to use React icons. L
4 min read
How to create Popup box in React JS ? Popup boxes, also known as modal or dialog boxes, are common UI elements used to display additional content or interactions on top of the main page. Creating a popup box in React can be achieved using various approaches, including libraries or custom implementations. We will use the Reactjs-popup li
2 min read
How To Create A Custom MUI Search Bar Component In NextJS? 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.Prerequ
3 min read
How to set state with a dynamic key name in ReactJS ? When working with ReactJS, managing state is a fundamental aspect of building dynamic and interactive user interfaces. In some cases, you may encounter situations where you need to set a state with a dynamic key name, rather than a fixed one. This can be particularly useful when dealing with dynamic
2 min read