Implementing the Stats component

Explore how we can improve search results and user experience by adding the number of hits based on user search by using the Stats component.

We'll cover the following


In the previous lesson, we added the highlight feature to our search functionality to improve the user experience and demonstrate the highlighted attribute based on the search query.

In this lesson, we will learn how to add the stats feature to our search functionality to improve the search experience further

The Stats component

The Stats component displays the total number of matching hits from our results. Also, we get the time it takes to receive the results.

import React from 'react';
import {
} from "react-instantsearch-dom";

class App extends React.Component {
	render() {
		return (
                  <Stats />
				  <Hits hitComponent={Hit} />

const Hit = (props) => {
  return (
      <div className="title">
      <div className="price">£{props.hit.price}</div>

Stats props

The translations prop takes a value of type object that is optional. This value is a mapping of keys to translations.

          stats(nbHits, timeSpentMS) {
            return nbHits > 0
                      ? `Clothing (${nbHits})`
                      : "no results found";

Get hands-on with 1200+ tech skills courses.