Search After Stop Typing React Component

In this post I’m going to go through how to implement a search criteria component that only invokes the search in a calling component after the user has stopped typing.

So, we have the following search criteria component that invokes the search on a submit button at the moment:

… and this component is referenced like the following in a calling component:

At the moment, our submit button triggers the form’s submit which in tern calls handleSubmit() which then calls this.props.doSearch in our calling component to invoke the search (our calling component actually does the search).

Here’s what our component looks like at the moment:

We want to remove the submit button and just have the search invoke when the user stops typing.

So, let’s first remove the form and submit button from our render function:

Now let’s just invoke the search in handleCriteriaChange after we have set the criteria in the state:

So, our component invokes the search on every keystroke that the user does:

Not quite what we want!

We need to “debounce” the search. That’s where the debounce function in the excellent lodash library comes in.

So, let’s bring lodash and the TypeScript types into our project:

We now need to import just the debounce function into our component:

If we look at the docs, debounce just creates another function that delays calling another function. So, let’s create our “debounce” function leaving the console.log() so that we can check that doSearch is only being called when the user has stopped typing:

… and let’s call our new function from handleCriteriaChange:

… and let’s give this a try:

… which is just what we want!

Here’s the full code for our debouncing search criteria component:

Connect with me:RSSGitHubTwitterLinkedIn

Formatting dates and numbers in React

Formatting numbers and dates is a common requirement for lots of apps but how do we do this in react apps?

Let’s say we have a component in our app that shows a list of customers, looking like this:

… and here’s the current code for this component:

… with the following data:

You can see that we need to clean up the revenue and first sale data by formatting them.

Formatting numbers

We can use Intl.NumberFormat to format the revenue:

In the above example we are formatting to revenue using Great British locale, using the Great British currency style.

So, we now have this:

But what if we don’t want the decimals in the revenue? We just add the min and max fraction digits in the options:

Formatting dates

We could just use toLocaleDateString instead of toString:

… which gives us a nice short date:

However, we can use Intl.DateTimeFormat to gain more control on the formatting:

Here we are specifying a 2 digit day, a long month and a 4 digit year.

So, here’s our final CustomerSearchResults component code:

… and here’s what it looks like:

Much better!

Connect with me:RSSGitHubTwitterLinkedIn

Building a simple component in React.js v2

I’ve been using React again recently. It’s come along way in the last couple of years since I last blogged on building a simple search component. In this post I’ll build this component again using TypeScript with modern React constructs …

This is a link to the old code and below is what it looked like:

Getting started

To scaffold the app I used TypeScript-React-Starter.

The key steps are installing create-react-app …

… and then running it, referencing some TypeScript scripts …

You can then use the following command to start the app …

Basic component structure

The app is going to consist of the following components as we had in v1:

  • Criteria – responsible for the display and entry of the search criteria
  • ResultsList – responsible for the display of the search results
  • Search – this wraps the Criteria and ResultsList components and performs the actual search
  • App – the top level component for our app (essentially just wrapping the Search component)

Criteria component

Let’s start with our Criteria component …

This is a functional component with no state.

It takes the current criteria value in as a property which is then bound to the criteria input value.

It also takes in a delegate function in as a property which is bound to the form’s submit event via our handleSubmit() function.

Finally it takes in a delegate function in as a property which is bound to the criteria input change event via our handleCriteriaChange() function.

We are also using the arrow function notation to avoid the “this” scoping problem.

ResultsList component

Let’s move on to our ResultsList component …

This is also a functional component with no state that takes in the customers to display as a property value.

We make use of to project the customers into a html list.

We also need to remember to include the key attribute so that React can properly manage changes to the html list.

The interface for Customer is:

Search component

Now on to our Search component …

This is our first component which has state which is why we are using a class component. Our state consists of:

  • allCustomers – a hardcoded array of all our customers
  • filteredCustomers – the current array of filtered customers
  • criteria – the current criteria

The state is intialised in the constructor with the filtered customers set to all the customers and the criteria set to an empty string.

The handleCriteriaChange() function catches changes to the criteria that are bubbled up from the Criteria component and sets the criteria bit of the state.

The doSearch() function catches the submit event from the Criteria component. It then does the appropriate filter on the array of customers using Array.prototype.filter(). Lastly it sets the filteredCustomers bit of the state.

The markup simply references the Criteria and ResultsList components, passing the appropriate properties.


Lastly, here’s our App functional component which simply wraps the Search component …

That’s it! The code certainly feels a lot cleaner than v1.

Connect with me:RSSGitHubTwitterLinkedIn