How to Create a Search Bar in React

Adding a search bar to your React application can greatly enhance its usability and provide a seamless experience for your users. In this article, we will explore step-by-step instructions on how to create a search bar in React, allowing users to search and filter through data effortlessly. Let’s dive in!

Prerequisites

Before we begin, make sure you have a basic understanding of React and have set up a React project. Familiarity with HTML, CSS, and JavaScript will also be helpful.

Step 1: Setting Up the Project

To create a search bar in React, we first need to set up our project. Assuming you have already created a React project, navigate to the component where you want to add the search bar.

Step 2: Creating the SearchBar Component

In the chosen component, create a new file called SearchBar.js and import it into your component file. Inside SearchBar.js, define a functional component named SearchBar:

import React from 'react';

const SearchBar = () => {
  return (
    <div>
      {/* Search bar code */}
    </div>
  );
};

export default SearchBar;

Step 3: Adding State for the Search Query

In order to store the user’s search query, we need to add state to the SearchBar component. Add the following code inside the SearchBar component:

const [searchQuery, setSearchQuery] = React.useState('');

This code uses the useState hook to create a state variable searchQuery and a function setSearchQuery to update its value.

Step 4: Handling User Input

Next, we need to handle the user’s input in the search bar and update the search query state accordingly. Add an onChange event handler to the search input field:

<input
  type="text"
  value={searchQuery}
  onChange={(e) => setSearchQuery(e.target.value)}
/>

The onChange event handler calls setSearchQuery with the current value of the input field, updating the searchQuery state.

Step 5: Filtering Data

Now that we have the user’s search query, we can use it to filter the data. Assuming you have data stored in an array, create a filtered version of the data based on the search query. Add the following code below the search input field:

const filteredData = data.filter(item =>
  item.name.toLowerCase().includes(searchQuery.toLowerCase())
);

Replace data with your actual data array, and item.name with the property you want to search against.

Step 6: Rendering the Results

To display the filtered results, we can map over the filteredData array and render the necessary components. Add the following code below the filtering code:

{filteredData.map(item => (
  // Render the necessary components for each item
))}

Replace the commented section with the appropriate code for rendering each item.

Step 7: Putting It All Together

Now that we have completed the individual steps, let’s integrate the SearchBar component into your main component:

import React from 'react';
import SearchBar from './SearchBar';

const MyComponent = () => {
  // Your component code

  return (
    <div>
      <SearchBar />
      {/* Render the results */}
    </div>
  );
};

export default MyComponent;

Make sure to replace MyComponent with the name of your actual component.

author's bio photo

Hi there! I am Avic Ndugu.

I have published 100+ blog posts on HTML, CSS, Javascript, React and other related topics. When I am not writing, I enjoy reading, hiking and listening to podcasts.