StateResults
About this widget
We released React InstantSearch Hooks, a new InstantSearch library for React. We recommend using React InstantSearch Hooks in new projects or upgrading from React InstantSearch.
The StateResults
widget provides a way to access the searchState
and the searchResults
of InstantSearch. For instance, this widget allows you to create results/no results or query/no query pages.
Customize the UI with connectStateResults
If you want to create your own UI of the StateResults
widget or use another UI library, you can use connectors.
Connectors are higher-order components. They encapsulate the logic for a specific kind of widget and they provide a way to interact with the InstantSearch context.
They have an outer component API that we call exposed props, and they provide some other props to the wrapped components which are called the provided props.
It’s a 3-step process:
// 1. Create a React component
const StateResults = () => {
// return the DOM output
};
// 2. Connect the component using the connector
const CustomStateResults = connectStateResults(StateResults);
// 3. Use your connected widget
<CustomStateResults />
Create a React component
const StateResults = ({
object searchState,
object searchResults,
object allSearchResults,
object error,
boolean searching,
boolean searchingForFacetValues,
boolean isSearchStalled,
}) => {
// return the DOM output
};
Provided Props
searchState
|
type: object
The |
||
Copy
|
|||
searchResults
|
type: object
In case of multiple indices, if used under |
||
Copy
|
|||
allSearchResults
|
type: object
In case of multiple indices, you can retrieve all the results. |
||
Copy
|
|||
error
|
type: object
Where the error is logged if the search fails. |
||
Copy
|
|||
searching
|
type: boolean
If there’s a search in progress. We recommend the usage of |
||
Copy
|
|||
searchingForFacetValues
|
type: boolean
If there’s a search for facet values in progress. |
||
Copy
|
|||
isSearchStalled
|
type: boolean
Whether InstantSearch has detected that searches are stalled. |
||
Copy
|
Create and instantiate your connected widget
const CustomStateResults = connectStateResults(StateResults);
<CustomStateResults />
Full example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { connectStateResults } from 'react-instantsearch-dom';
const StateResults = ({ searchResults }) => {
const hasResults = searchResults && searchResults.nbHits !== 0;
const nbHits = searchResults && searchResults.nbHits;
return (
<div>
<div hidden={!hasResults}>There are {nbHits} results</div>
<div hidden={hasResults}>There is no results</div>
</div>
);
};
const CustomStateResults = connectStateResults(StateResults);