The Great Search [bar] - Part 2 - Deep Dive
Tags: beginners, react, javascript
Published: 2020-01-15 18:48:08

---
title: The Great Search [bar] - Part 2 - Deep Dive
published: true
description: Creating a React Search Bar from scratch.
tags: beginners, react, javascript
series: The Great Search [bar]
cover_image: https://images.unsplash.com/photo-1518306896927-d58c59c07b82?ixlib=rb-1.2.1
---
 Part one was more about the overview of why I bothered to create my search bar instead of copying other code. This time, I’ll focus on the technical or how and why I implemented it. 

### Retrieval
 Before I even started with the search bar, I needed to ensure the component wouldn’t break if the users’ list took a bit longer to fetch. Slower networks may not respond in a desirable amount of time. After beginning the fetch request for the list, I used a simple ‘if’ statement with a more user-friendly react-bootstrap spinner component, if the fetch was still completing.

 ![](https://thepracticaldev.s3.amazonaws.com/i/947x7zeap52x9o502ii4.png)

### Display
 After retrieving the current lesson for the user. The search bar needs to render and connect to the component state for later use.

 I used a simple form to display the bar, with a bit of react-bootstrap for styling and positioning. 
 ![](https://thepracticaldev.s3.amazonaws.com/i/7qg172ji8on10w65bdw3.png)

 With the state holding the search term, or last name:
 ![](https://thepracticaldev.s3.amazonaws.com/i/butfl592uk8e329f5h6v.png)
 
 While the state updates with every keystroke by the user:
![](https://thepracticaldev.s3.amazonaws.com/i/zxbmrpx3g25lhppgai1v.png)

 The first iteration of the search bar required the last name to be input and submitted before the search began. As the form is submitted, it invokes the function ‘handleSubmit’:
 ![](https://thepracticaldev.s3.amazonaws.com/i/rp9ee9zdo9srynwmgvx1.png)
 
 Once I had completed the search bar functionality, I found it very cumbersome and not as intuitive for the user as I would have liked. Whenever I do create something that requires user interaction, I prefer it to feel effortless and, if possible, familiar. So I added a function that we’ll see later on in another component which rendering invokes. With this, the search would perform with each input change. Thus, giving the user faster and distinct confirmation of the search functioning.

 Then the important step. Rendering the results and giving the option to check-in. I felt this was a good point to separate the concerns of rendering the interface and the actual searching. We’ll get back to why `this.props.history` passes to `<SearchResults/>`
 ![](https://thepracticaldev.s3.amazonaws.com/i/6vbwxj2r16f6wra8ob5b.png)

### Search
 Before I built the search bar, I rendered another component,`<RetrieveCurrentLesson/>`, which (you guessed it) renders the current lesson. Understanding how it functions isn’t as important as it’s output. If there were no lessons for the day, it would display ‘No lessons today.’ Otherwise, the current lesson would display with start & end times. The code responsible for this:

 ![](https://thepracticaldev.s3.amazonaws.com/i/xascryof92dkz7q08euc.png)
 
 I chose this way for a few reasons:
1. It showed the user there were ‘No Lessons’ instead of blank or undefined.
2. I thought there would be more confusion if I chose the next lesson to display even if it wasn’t for that day. Even though theoretically, there should be no students if there are no classes. I was building for a less specific and more scaled use. 
3. I could leverage the response of ‘No Lessons’ for the next component.

If there were no lessons for the day, I had to ensure that not only was it stated, but there was also no way to check-in by accident. So, as the component renders, it invokes a simple function to determine whether the check-in button should be enabled or not.
![](https://thepracticaldev.s3.amazonaws.com/i/jroyjul1umca7oqxd7h6.png)
  
After handling the no lesson errors, the search functionality comes next. When the component renders, it invokes `this.filterUsers(this.props)` and maps over the output to display the user that matches the search term. The function itself filters the user list and compares the last name to the search term.

With the selected user now being displayed with first name, last name, and rank. The button to check-in and attach the user displays with the following code `<Button variant="success" disabled={!isEnabled} onClick={() => this.attachUser(user)}> Check-In </Button>`:

![](https://thepracticaldev.s3.amazonaws.com/i/8yi4o3ai8pub6jeinaxt.png)

### Association
  When the button is enabled and clicked, it invokes the `attachUser` function and passes the user as an argument. attachUser has a few responsibilities.
  
  ![](https://thepracticaldev.s3.amazonaws.com/i/ndotz1peooltul5qk65y.png)
  
  First, it grabs the currentLesson from the redux store or props. It can then add the user to the list of users associated with the lesson and invoke the associateUser action, made available through redux `export default connect(state => ({ currentLesson: state.currentLesson }), {associateUser})(SearchResults)`.
  
  As you can see, associateUser is a pretty standard fetch asynchronous request. It updates the provided lesson and expects the updated lesson back as the payload. I also added the history prop we previously passed down, which allows me to use this: `window.location.reload(false)`. It reloads the page, leaving it ready for the next user to check in with a cleared search bar. The 'false' argument tells `window.location.reload` to reload from cache if possible. Reloading with cache is faster and won’t affect the search bar functionality.
  
### Conclusion
  I enjoyed going through each step and having to research every option to decide what was best for my situation.
  
  In the future, I’d like to experiment with large data sets to see if the constant rendering of components due to user input change, makes a performance difference. With the small data set I used and the overall better user experience, I preferred this way. 
  
  If you have any questions or comments on how I can improve this, let me know! I’m a ~~junior~~ developer, I’m always learning and improving.

### Code Repo

If you'd like to check out more of the code or suggest improvements. Here's the repo:

{% github dclements9/Hara-Frontend no-readme %}

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