Clearing the hits

Hi there

I have a offcanvas search bar, and I am only triggering a search when the user enters a query, using

if (helper.state.query) {
   helper.search();
}

I am trying to clear the returned results and the query when the user closes the search bar.

I came across the suggestion to use helper.setQuery("").search attached to an event, but while this does clear helper.state.query, it doesn’t actually clear the displayed hits or the text in the search input.

My hope was that it would clear the query and then run another search, returning no results.

Any ideas where I am going wrong? Is it because the app is configured not to run a search without a query?

Thanks!

Hi Mark,

An empty query actually returns all results from an index. You might be better off tracking the state for an empty query and hiding the results container. This pattern is described here:

Yes, thanks, that seems to be the best approach.

1 Like