logo

Education

How to Build React Search Bar with Suggestions for Enhanced User Experience

Authore Name
Shubham Jasani

Software Development Executive - II

Last updated on Feb 19, 2024

When developing a modern web application, enhancing the user experience is paramount. One way to achieve this is by implementing a react search bar with suggestions. This feature not only improves the usability of your app but also guides users through their searches, making the process faster and more intuitive. If you're wondering how to create such a dynamic and responsive search box in ReactJS, you've come to the right place.

Understanding the Basics of React Search Autocomplete

Before diving into the code, let's establish what an autocomplete component is. Essentially, it's an input field that provides real-time suggestions to the user as they type. These suggestions are typically based on an array of data that can be filtered according to the string searched. React search autocomplete components are highly customizable, allowing you to tailor the look and functionality to your app's needs.

Setting Up Your React Project for Search Autocomplete

Getting Started with the Project Initialization

To begin, you'll need to set up a new React project. Once you have your project directory, navigate to it in your terminal and run the following command to create a new React app:

1npx create-react-app autocomplete-search-app 2

This command scaffolds a new React project with all the necessary files and folder structure you'll need. Navigate to your project directory, and you're ready for the next step.

Structuring Your React Application

Now that we have our React app, let's discuss the folder structure. A tidy folder structure ensures that our components and utilities are organized and easily accessible. Create folders for components, styles, and utils in your project's src directory**.** This will help us manage our autocomplete component, CSS files, and helper functions.

Installing the Autocomplete Package

With our folder structure in place, it's time to add the react-search-autocomplete package to our project. Run the following command in your project directory:

1npm install react-search-autocomplete 2

This command fetches the latest version of the react-search-autocomplete package and adds it to our project's dependencies. Now we're all set to import and use it in our app.

Preparing the App Component

Before we dive into the autocomplete component, let's prepare our main app component. Open the App.js file in your editor. Here, we'll import React and set up the basic structure of our app. The app header will be the first component users see, so let's make it welcoming.

1import React from 'react'; 2import './App.css'; // Import your main CSS file to style the app 3 4function App() { 5 return ( 6 <div className="App"> 7 <header className="App-header"> 8 {/* We'll add our search component here soon */} 9 </header> 10 </div> 11 ); 12} 13 14export default App; // Don't forget to export your app component! 15

In the code above, we've prepared a functional component for our app and included a placeholder for where our search component will eventually live. The export default App statement makes our App component available for use in other parts of our project.

Crafting the Search Component with Autocomplete

Creating the Input Field

The heart of our search functionality lies in the input field, where users will type their queries. Let's create this essential part of our app. In the components folder, make a new file called SearchBar.js. This file will house our autocomplete component.

1import React from 'react'; 2import { ReactSearchAutocomplete } from 'react-search-autocomplete'; 3 4function SearchBar() { 5 // We'll add the logic for handling searches here soon 6 7 return ( 8 <div style={{ width: '100%' }}> 9 {/* Our autocomplete component will go here */} 10 </div> 11 ); 12} 13 14export default SearchBar; 15

We've set up a functional component to render our search bar in the snippet above. Notice how we've prepared a space to include our autocomplete component, which we'll get to shortly.

Integrating the Autocomplete Component

Now, let's integrate the autocomplete component into our SearchBar component. The autocomplete component will display suggestions to the user as they type, making the search experience intuitive and efficient.

1import React from 'react'; 2import { ReactSearchAutocomplete } from 'react-search-autocomplete'; 3 4function SearchBar() { 5 const items = [ 6 { 7 id: 0, 8 name: 'HTML' 9 }, 10 { 11 id: 1, 12 name: 'JavaScript' 13 }, 14 { 15 id: 2, 16 name: 'Basic' 17 }, 18 { 19 id: 3, 20 name: 'PHP' 21 }, 22 { 23 id: 4, 24 name: 'Java' 25 } 26 ]; 27 28 return ( 29 <div style={{ width: '100%' }}> 30 <ReactSearchAutocomplete 31 items={items} 32 // Additional props and event handlers will be added here 33 /> 34 </div> 35 ); 36} 37 38export default SearchBar; 39

We've imported the ReactSearchAutocomplete component in the code above and included it in our SearchBar component's render method. The items array will eventually contain the suggestions we want to display to the user.

A well-designed search box is crucial for user engagement. Let's add some CSS to make our search input field stand out. Create a SearchBar.css file in the styles folder and import it into your SearchBar.js file.

1/* SearchBar.css */ 2.search-bar-container { 3 max-width: 500px; 4 margin: auto; 5} 6
1import React from 'react'; 2import { ReactSearchAutocomplete } from 'react-search-autocomplete'; 3import './SearchBar.css'; // Importing our CSS file 4 5function SearchBar() { 6 const items = [ 7 { id: 0, name: 'HTML' }, 8 { id: 1, name: 'JavaScript' }, 9 { id: 2, name: 'Basic' }, 10 { id: 3, name: 'PHP' }, 11 { id: 4, name: 'Java' } 12 ]; 13 14 return ( 15 <div className="search-bar-container"> 16 <ReactSearchAutocomplete 17 items={items} 18 // ...previous props 19 /> 20 </div> 21 ); 22} 23 24export default SearchBar; 25

With the CSS imported, our search bar will now have a maximum width and be centered on the page, thanks to the styles we defined.

Enhancing User Interaction

We want to provide immediate and relevant feedback when users interact with the search bar. This is where event handlers come into play. We'll add functions to handle search input, item selection, and hovering.

1import React, { useState } from 'react'; 2import { ReactSearchAutocomplete } from 'react-search-autocomplete'; 3import './SearchBar.css'; 4 5function SearchBar({ onSearchSelected }) { 6 const [items, setItems] = useState([ 7 { id: 0, name: 'HTML' }, 8 { id: 1, name: 'JavaScript' }, 9 { id: 2, name: 'Basic' }, 10 { id: 3, name: 'PHP' }, 11 { id: 4, name: 'Java' } 12 ]); 13 14 const handleOnSearch = (string, results) => { 15 // Triggered when the user types in the search input 16 console.log(string, results); 17 }; 18 19 const handleOnHover = (item) => { 20 // Triggered when the user hovers over an item in the suggestions list 21 console.log('Item hovered:', item); 22 }; 23 24 const handleOnSelect = (item) => { 25 // Triggered when the user selects an item from the suggestions list 26 console.log('Item selected:', item); 27 onSearchSelected(item); 28 }; 29 30 return ( 31 <div className="search-bar-container"> 32 <ReactSearchAutocomplete 33 items={items} 34 onSearch={handleOnSearch} 35 onHover={handleOnHover} 36 onSelect={handleOnSelect} 37 /> 38 </div> 39 ); 40} 41 42export default SearchBar; 43

In the updated code, we've added three functions: handleOnSearch, handleOnHover, and handleOnSelect. These functions will be called when the user searches for a string, hovers over a suggestion, and selects a suggestion.

Connecting the Autocomplete Component with Backend Services

Setting Up API Communication

Our autocomplete component needs to communicate with a backend service to provide real-time suggestions. This could be an internal API or a third-party service, like Google Maps API, depending on the use case. Let's set up the basic structure for API requests within our SearchBar component.

1import React, { useState, useEffect } from 'react'; 2import { ReactSearchAutocomplete } from 'react-search-autocomplete'; 3 4function SearchBar() { 5 const [searchTerm, setSearchTerm] = useState(''); 6 const [suggestions, setSuggestions] = useState([]); 7 8 useEffect(() => { 9 if (searchTerm.trim() === '') { 10 setSuggestions([]); 11 } else { 12 fetchSuggestions(searchTerm); 13 } 14 }, [searchTerm]); 15 16 const fetchSuggestions = async (searchTerm) => { 17 try { 18 // Replace 'YOUR_API_ENDPOINT' with the actual endpoint of your API 19 const response = await fetch(`YOUR_API_ENDPOINT?query=${encodeURIComponent(searchTerm)}`); 20 const data = await response.json(); 21 setSuggestions(data); // Assuming the API returns an array of suggestions 22 } catch (error) { 23 console.error('Error fetching suggestions:', error); 24 } 25 }; 26 27 const handleOnSearch = (string) => { 28 setSearchTerm(string); 29 }; 30 31 const handleOnSelect = (item) => { 32 console.log('Selected:', item); 33 }; 34 35 const handleOnHover = (item) => { 36 console.log('Hovered:', item); 37 }; 38 39 const handleOnFocus = () => { 40 console.log('The search input is focused'); 41 }; 42 43 const handleOnClear = () => { 44 console.log('The search input is cleared'); 45 setSuggestions([]); 46 }; 47 48 return ( 49 <div style={{ width: 300 }}> 50 <ReactSearchAutocomplete 51 items={suggestions} 52 onSearch={handleOnSearch} 53 onSelect={handleOnSelect} 54 onHover={handleOnHover} 55 onFocus={handleOnFocus} 56 onClear={handleOnClear} 57 placeholder="Type to search" 58 /> 59 </div> 60 ); 61} 62 63export default SearchBar; 64

In the code snippet above, we've added a useState hook to manage our suggestions state and a fetchSuggestions function that simulates an API call to fetch suggestions based on the user's input.

Updating the Autocomplete with API Data

Once we have our suggestions from the API, we need to update our autocomplete component to display these suggestions. We'll modify the handleOnSearch function to call fetchSuggestions whenever the user types in the search input.

1import React, { useState } from 'react'; 2import { ReactSearchAutocomplete } from 'react-search-autocomplete'; 3import './SearchBar.css'; 4 5function SearchBar({ onSearchSelected }) { 6 const [suggestions, setSuggestions] = useState([]); 7 8 const fetchSuggestions = async (searchTerm) => { 9 try { 10 // Replace with your API call logic 11 const response = await fetch(`YOUR_API_ENDPOINT?query=${searchTerm}`); 12 const data = await response.json(); 13 setSuggestions(data); // Update our suggestions state with the API response 14 } catch (error) { 15 console.error('Error fetching suggestions:', error); 16 } 17 }; 18 19const handleOnSearch = (string, results) => { 20 // Trigger the API call to fetch suggestions 21 fetchSuggestions(string); 22 }; 23 24 return ( 25 <div className="search-bar-container"> 26 <ReactSearchAutocomplete 27 items={suggestions} 28 /> 29 </div> 30 ); 31} 32 33export default SearchBar; 34

By calling fetchSuggestions inside handleOnSearch, we ensure that our autocomplete suggestions are always up to date with the latest data from our backend service.

Fine-Tuning the Search Experience

To further refine the user experience, we can debounce the search input to reduce the number of API calls made while the user is typing. This improves performance and reduces unnecessary load on the server.

1import { debounce } from 'lodash'; 2 3function SearchBar() { 4const [suggestions, setSuggestions] = useState([]); 5 6 // Debounce the fetchSuggestions function 7 const debouncedFetchSuggestions = debounce(fetchSuggestions, 300); 8 9 const handleOnSearch = (string, results) => { 10 // Use the debounced version of our API call function 11 debouncedFetchSuggestions(string); 12 }; 13 14 // ...other event handlers and return statement 15} 16

In the updated handleOnSearch function, we've introduced a debounced version of fetchSuggestions using the debounce function from Lodash, which will wait for 300 milliseconds after the last keystroke before making the API call.

Enhancing the Search Bar with Advanced Features

Customizing the Autocomplete Behavior

To make our search bar stand out, we need to customize the autocomplete component to suit our users' needs better. This involves tweaking the behavior and appearance of the suggestions displayed. For instance, we might want to show more information about each suggestion or change how they're rendered based on user interactions.

Let's add a custom rendering function to format our results. This function will allow us to display additional details about each suggestion, such as an id or other relevant information.

1function SearchBar() { 2 // ...previous code and state 3 4 const formatResult = (item) => { 5 return ( 6 <> 7 <span style={{ display: 'block', textAlign: 'left' }}>ID: {item.id}</span> 8 <span style={{ display: 'block', textAlign: 'left' }}>Name: {item.name}</span> 9 </> 10 ); 11 }; 12 13 // ...other event handlers and return statement 14} 15

In the formatResult function above, we're customizing how each result is displayed by including the item's id and name. This function can be passed as a prop to our ReactSearchAutocomplete component.

Optimizing User Experience with Visual Feedback

Visual feedback is key when a user interacts with the search bar. Let's implement visual cues for when a user hovers over an item and selects an item. These interactions can be logged to the console or used to trigger other actions within the app.

1function SearchBar() { 2 // ...previous code and state 3 4 const handleOnHover = (item) => { 5 // Log the item hovered to the console 6 console.log('Item hovered:', item); 7 }; 8 9 const handleOnSelect = (item) => { 10 // Log the item selected to the console 11 console.log('Item selected:', item); 12 }; 13 14 // ...other event handlers and return statement 15} 16

The handleOnHover and handleOnSelect functions provide immediate feedback to the user. They can further enhance the search experience, such as displaying additional details about the hovered or selected item.

Implementing Accessibility and Responsive Design

Accessibility and responsive design ensure that our search bar is usable by everyone, regardless of their device or abilities. We can customize the styles of our autocomplete component to ensure it is both accessible and responsive.

1function SearchBar() { 2 // ...previous code and state 3 4 const styling = { 5 // Customize the styles of the autocomplete component 6 height: '40px', 7 border: '1px solid #dfe1e5', 8 borderRadius: '4px', 9 backgroundColor: 'white', 10 boxShadow: 'none', 11 hoverBackgroundColor: '#f2f2f2', 12 color: '#212121', 13 fontSize: '14px', 14 fontFamily: 'Helvetica, sans-serif', 15 // Additional styling can be added here 16 }; 17 18 // ...other event handlers and return statement 19} 20

By providing a styling object, we can define the look and feel of our search bar, ensuring it adapts well to different screen sizes and is easy to navigate for users with accessibility needs.

Final Thoughts

As we conclude our journey of implementing a React search bar with autocomplete suggestions, we've seen how combining modern React practices and the react-search-autocomplete package can create a powerful and user-friendly search experience. From setting up our project and crafting a responsive input field to connecting with backend services and fine-tuning the user interactions, each step has brought us closer to a feature-rich search component.

Remember, the code snippets provided throughout this blog are just the beginning. You can customize and extend the functionality to match the specific needs of your users and your application. The possibilities are endless, Whether by enhancing the visual design, integrating with different APIs, or adding more complex filtering logic.

With your search bar now in place, you're now equipped to build an intuitive search feature that serves your users' needs and elevates your app's overall user experience.

Short on time? Speed things up with DhiWise!!

Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!

You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.

Sign up to DhiWise for free

Read More