The Great Search [bar]- Part 1 - Context
Tags: beginners, react, javascript
Published: 2020-01-09 03:14:45

---
title: The Great Search [bar]- Part 1 - Context  
published: true
description: React implementation of a search bar
tags: beginners, react, javascript
series: The Great Search [bar]
cover_image: https://images.unsplash.com/photo-1473163928189-364b2c4e1135?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9.jpg
---
*I’m new to writing blog posts. So I’m trying something out. I’m splitting the more technically detailed portion from the less technically. Feel free to let me know if it feels silly to be separated or keeps it more coherent. Part 2 - Deep Dive coming soon.* 

While a simple Google search reveals many ways to create a Search Bar with **React & Javascript**, I wanted to create my own to understand how it works. If you’d like to skip the extended version, head to **'The Implementation’** section or check out the technically detailed post coming on Tuesday. 

## The Project
 My most recent project was creating a React client for a Rails API. I created an uncomplicated martial arts studio management system. The administrator can create users/students, lessons/classes, and student challenges. While the student side would be able to sign in when they attend class.

 For the student sign-in, I planned to implement a clean and intuitive search bar. The student would type their last name, and their information would populate with the option to check-in. Keeping it simple, easy, and to the point was my main focus. Unfortunately, I had no idea where to start. Like my time with The Flatiron School had taught me, I started with what I knew:

 1. The search bar had to be able to fetch all the users
 2. After retrieving the users, it would have to filter the users
 3. It would then need to display the user’s information clearly for the user to read.
 4. It would also need to display a functional button to check the user in
 5. The current lesson would need to be retrieved to be attached to the selected user.
 6. Ideally: It would be able to do this all while the user was still typing

## The Knowledge
 All of this felt reasonable to achieve, and while I wrote the pseudo-code, it started to come together. I knew how it *should* function, but I was hazy on how the implementation. So as any developer does when confronted with a lack of knowledge, I went to Google. Unsurprisingly, there are many methods people use to implement a search bar.
 
 While I read through the various techniques, I could understand the processes, but I couldn’t understand the reasons for their designs or why I would prefer one over the other. I didn’t feel comfortable using code I couldn’t fully explain the reason behind using. So I decided to create my own.
 ![](https://images.unsplash.com/photo-1558420034-e98aa2ce8cac?ixlib=rb-1.2.1)
## The Implementation

### First Step - Search Bar
The search bar renders a form for user input assigned to the searchTerm state.

### Second Step - Current Lesson
On the initial render of the home page component,  `<RetrieveCurrentLesson />` retrieves the current lesson. Once mounted, the component fetches all lessons. It then filters the lessons by date & time to find the nearest lesson to the current date & time. Afterwards, `<DisplayCurrentLesson lesson={nextLesson} />` is called to handle displaying the contents.

### Third Step - State
As the user input changes (the user enters or removes a character), the searchTerm state is updated:
 `handleChange = (e) => { this.setState({ [e.target.name]: e.target.value })}`

This state is then passed to another component: `<SearchResults searchTerm={this.state.searchTerm} />`

### Fourth Step - Search
The users list passes to the SearchResults component after being fetched from the home page component. Much like the current lesson, the user is then filtered based on the searchTerm state from User input.

As a user’s last name matches a user in the users list, it is displayed along with a button to ‘Check-in’.

### Fifth Step - Association
The button rendered with the searched user associates the user with the current lesson. The home page then renders a cleared form, ready for the next user to ‘Check-In’.

## Conclusion
As they say, “There’s more than one way to skin a cat.” Ultimately, I’m glad I went through the trouble of creating my method. It forced me to understand the workflow and design. Even if it’s not the most efficient method, I’m proud of it and can always improve. 



### Attribution
Cover Photo by [Andrew Neel](https://unsplash.com/@andrewtneel?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)

Second Photo by [Evgeni Tcherkasski](https://unsplash.com/@evgenit?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)