Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to create search box in ReactJS

Introduction

Hello, dear reader! Today we will embark on a journey to create a search box in ReactJS. If you're new to programming or just learning about ReactJS, don't worry. We are going to walk through this together.

For any beginner, the word 'ReactJS' may sound a bit intimidating. But let's break it down. ReactJS is simply a JavaScript library used to build user interfaces, particularly single-page applications where you can stay on one page, but see a lot of content by simply interacting with what's on the screen. Think of it like a book - all the pages are right there in your hand, but you only see one at a time.

Now, let's get started with our project.

Setting Up Your Environment

Before we dive in, you'll need to have NodeJS and npm (Node Package Manager) installed on your machine. NodeJS is just a way for us to run JavaScript outside of the browser (like on your computer), and npm is a tool that helps us manage packages (bits of code) that we need for our application. Think of it as a toolbox for your project.

You can download NodeJS and npm from the official website. Once installed, you can check the versions by typing node -v and npm -v in your terminal.

Creating a New React App

With NodeJS and npm ready, we can create our new ReactJS app. We'll use the Create React App tool, which sets up a new project with a good default configuration. It's like a chef preparing all the ingredients for you.

In your terminal, navigate to the folder where you want your project to be located and type:

npx create-react-app search-box

This command is telling your computer to use npx (a tool that makes it easy to use packages) to create a new React app called "search-box".

This process may take a few minutes. Once it's done, navigate into your new project folder by typing cd search-box in your terminal.

Building the Search Box Component

In ReactJS, everything is built using components. Components are like the building blocks of our application. In our case, the search box will be a component.

In your project folder, navigate to the 'src' directory and create a new file called 'SearchBox.js'. This is where we'll build our SearchBox component.

Your SearchBox component will start like this:

import React from 'react';

class SearchBox extends React.Component {
  render() {
    return (
      <div>
        Search Box
      </div>
    );
  }
}

export default SearchBox;

This is the basic structure of a component in ReactJS. The 'import' and 'export' lines allow us to use this component in other parts of our application. The 'render' method is what actually gets displayed on the screen. Right now, it's just displaying the text "Search Box".

Adding an Input Field

To make our component work like a search box, we need an input field where users can type their search queries. We'll use the HTML 'input' element for this.

Replace the 'render' method with the following:

render() {
  return (
    <input type="text" placeholder="Search..." />
  );
}

Here, 'type="text"' means that the input field will accept text. The 'placeholder' attribute is just the grey text that shows up in the input field before anything is typed.

Handling User Input

So far, we have a search box, but it doesn't actually do anything. Let's add functionality to handle user input.

First, we'll add a method to our component that will handle changes in the input field. This method will be called every time the user types something into the search box.

Add the following method to your SearchBox component:

handleInputChange = (event) => {
  console.log(event.target.value);
}

This method receives an 'event' object which contains information about the user's interaction with the input field. event.target.value gives us the current text in the input field.

Next, we need to tell our input field to call this method whenever the user types something. This is done using the 'onChange' attribute. Modify your 'render' method like this:

render() {
  return (
    <input 
      type="text" 
      placeholder="Search..." 
      onChange={this.handleInputChange}
    />
  );
}

Now, every time you type something in the search box, the text should be logged to the console.

Wrapping Up

And that's it! You have now created a functional search box in ReactJS. Of course, this is a very basic example and there's a lot more you can do. But hopefully, this guide has helped you understand the basics of working with ReactJS and how components work.

Remember to always experiment, try new things, and don't be afraid to make mistakes. Happy coding!