A search bar is a common feature in web applications that allows users to quickly find specific information within a dataset. In this blog post, we'll explore how to create a search bar in a React.js application. By the end of this tutorial, you'll have a functional search bar that filters data as users type.
Prerequisites
Before we start, make sure you have the following prerequisites in place:
-
Node.js and npm: Ensure that you have Node.js and npm (Node Package Manager) installed on your computer. You can download them from nodejs.org.
-
React Project: You should have an existing React project or create a new one. You can create a new React app using Create React App or any other setup of your choice.
Step 1: Create a React App (If Needed)
If you don't have a React project, you can create one using Create React App. Open your terminal and run the following command:
npx create-react-app search-bar-app
You can also use :
npm init react-app search-bar-app
Replace search-bar-app
with your preferred project name.
If you are having an error due to babel plugin, this code :
npm install --save-dev @babel/plugin-proposal-private-property-in-object
Step 2: Set Up the Data
For this example, let's assume you have a dataset you want to search. You can use a simple JavaScript array as your data source. Create a file named data.js
in your project's src
directory and define your data:
Step 3: Create the SearchBar Component
Now, let's create a SearchBar
component that allows users to search for data within your dataset. In your src
directory, create a new file named SearchBar.js
:
In this component:
- We use the
useState
hook to manage thesearchTerm
state, which represents the text entered in the search bar. - The
handleInputChange
function is called whenever the user types in the input field. It updates thesearchTerm
state and calls theonSearch
prop, which we'll define later to handle the actual search logic.
Step 4: Create the SearchableList Component
Now, let's create a SearchableList
component that displays the filtered data based on the search term. Create a file named SearchableList.js
in your src
directory:
In this component:
- We import the
SearchBar
component we created earlier and pass it thedata
array and thehandleSearch
function as props. - The
handleSearch
function filters the data based on the search term and updates thefilteredData
state. - The filtered data is then displayed as a list.
Step 5: Use the SearchableList Component
Now, let's use the SearchableList
component in your main App.js
file:
Step 6: Styling (Optional)
You can add CSS styles to your search bar and list components to improve their appearance. Create a CSS file, such as SearchableList.css
, in your src
directory and define your styles:
.
Step 7: Running the Application
Start your React application by running:
You should see your search bar and list displayed in the browser. You can now enter search terms in the input field to filter the list.
Conclusion
In this blog post, we've created a simple search bar component in a React.js application. This search bar allows users to filter data based on their input. You can customize this example to fit your specific data and styling needs. Implementing a search feature can greatly enhance the usability of your application, especially when dealing with large datasets.