Autocomplete Input in React

javascript react autocomplete input data feching
Autocomplete Input in React

Overview

This is the first lab in the series of cool React mini apps/components that we are going to build together. I have tried to keep it simple and at the same time informative enough so that you folks actually learn about how to create a custom autocomplete input box in React.

Features of the component

  1. There is an input box with placeholder
  2. On focussing on the input, a list of pokemons with their name and image appear in the div below.
  3. You can type something in the input and the list will get filtered accordingly
  4. On selecting an item in the list, the list disappears with the name of the pokemon in the input.
  5. Nice to have features:
    1. If the list of pokemons is visible, clicking on the input or any where in the page, the list should disappear and should reappear again when focussing on the input

 

Learning Objectives

In this lab, you will learn about different React concepts like hooks, creating a function component, data fetching in React, usage of array functions like map, fill, filter, etc.

 

To get the most out of this series:

You need to be at least acquainted with basic JS and React concepts. You will be able to understand everything if you have given atleast a day to learn React.

 

Code/Environment Setup:

Preferred Code Editor: VS Code,

Node version >= 12.0.0

Create a react project boilerplate using the create-react-app CLI or if you have npx installed on your system you can directly do npx create-react-app

To get npx on your system, run

npm install -g npx
npx create-react-app custom-autocomplete-react
cd custom-autocomplete-react
yarn start or npm start

and there you go!

You have your react project ready to be consumed and altered into something meaningful.

Handle outside click for options

The option display should disappear (removed from the DOM altogether) when the user clicks anywhere on the outside.

To do this, let us point a ref to the outermost div of the component and add an eventListener to the window object. If the mouseDown event appears on the window and the event.target is not the ref we created, shouldDisplayOptions will be set to false.

const wrapperRef = useRef(null);

useEffect(() => {
    window.addEventListener("mousedown", handleClickOutside);
});

const handleClickOutside = event => {
    const wrapper = wrapperRef.current;
    if (wrapper && !wrapper.contains(event.target)) {
      setShouldDisplayOptions(false);
    }
};

return (
  <div ref={wrapperRef} className="flex-container flex-column pos-rel">
     { /* rest of the code */ }
  </div>
)

But, there is an issue. In this scenario, even if the component is unmounted, the window object will have the reference of the eventListener that we attached to it. This will cause a memory leak in our application, that we never want as it will make our app non-performant eventually.

How to solve this?

useEffect React hook also provides a cleanup function, which is similar to componentWillUnmount lifecycle method in Class components.

The code inside useEffect will change to

useEffect(() => {
    window.addEventListener("mousedown", handleClickOutside);
    return () => {
      window.removeEventListener("mousedown", handleClickOutside);
   };
});

Handling the outside click will make your component behave like this

The final code can be found here

Discussion

3