How to Build a Custom React Search Bar Component using a Dynamic Regex

How to Build a Custom React Search Bar Component using a Dynamic Regex

In this tutorial, you'll see How to Build a Custom React Search Bar Component using a Dynamic Regex. Learn to build a simple, custom search bar component that uses a dynamic regex to filter search results based on the search text input.

As of lately, I’ve been in the process of building a progressive web app for my Mom’s Christmas present this year using React Hooks; It’s a digitalized version of her cookbooks and recipes all in one place.

With how many recipes she has, it got me thinking, if she wanted to find a single recipe, simply alphabetizing them might not be as intuitive as I would like.

This inspired me to create a search bar that would filter the recipe search results as the user was typing, instead of upon hitting a button.

Below, take a look at the end goal we are trying to achieve. Hopefully it is obvious this is just a template, and has fake recipes — my mom does not have a recipe called “Weird Stuff”!.

Notice how it only shows dishes with the current value (typed characters) somewhere in the name of the dish

In this post, we’ll discuss how I was able to make the search bar, and implement it into the app.

There are a few parts to this, and I’ll be going into each of these topics:

  1. Create the SearchBar component
  2. Include the necessary state using Hooks in our App component (pass as props to the SearchBar component)
  3. Make a dynamic regular expression that uses the state of the search bar input to match against each recipe.
  4. With a React Hook (useEffect), call a helper function to filter the results on every change of input state using the regex.

If you would like to take a look at the project for yourself, I have linked to the Github repository here. Also, my way is only one way to make something like this, I’m positive there are numerous other possibilities, and plenty of those are probably better ways too. Now, let’s dive into the first step!

react-hook regex react web-development javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

Hire Dedicated React Native Developer

Have you ever thought of having your own app that runs smoothly over multiple platforms? React Native is an open-source cross-platform mobile application framework which is a great option to create mobile apps for both Android and iOS. **[Hire...

Hire Dedicated React Native Developer in India | React Native Development

Hire dedicated React Native developers for your next project. As the top react native development company we offer you the best service as per your business needs.

Learn React Redux With React Hooks - React JavaScript Tutorial

In this video I wanted to show the basics of Redux in React, I didn't go over anything complex as I wanted this to be a beginners introduction into Redux. Let me know in the comments if you would like to see more content on redux such as Redux Thunk.

Hire React Js Developer from Expert React JS Development Company

Are you looking to [hire React JS developer](https://www.mobiwebtech.com/react-js-development-company/ "hire React JS developer") from a prestigious and reliable React JS development company? Visit at Mobiweb Technologies here we have a big team...