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.

Recommended reading for building great React apps: