<Hits>
<Hits // Optional props hitComponent={({ hit }) => JSX.Element} classNames={object} ...props={ComponentProps<'div'>} />
1
import { Hits } from 'react-instantsearch-hooks-web';
About this widget
<Hits>
is a widget that lets you display a list of results.
To configure the number of retrieved hits, use the <HitsPerPage>
widget or pass the hitsPerPage
prop to the <Configure>
widget.
You can also create your own UI with
useHits()
.
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import React from 'react';
import algoliasearch from 'algoliasearch/lite';
import { InstantSearch, Hits } from 'react-instantsearch-hooks-web';
const searchClient = algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey');
function Hit({ hit }) {
return JSON.stringify(hit);
}
function App() {
return (
<InstantSearch indexName="instant_search" searchClient={searchClient}>
<Hits hitComponent={Hit} />
</InstantSearch>
);
}
Props
hitComponent
|
type: (props: { hit: THit; sendEvent: SendEventForHits }) => JSX.Element
Optional
A component that renders each hit from the results. It receives a When not provided, the widget displays the hit as a JSON string. |
||
Copy
|
|||
escapeHTML
|
type: boolean
default: true
Optional
Whether to escape HTML tags from hits string values. |
||
Copy
|
|||
transformItems
|
type: (items: object[], metadata: { results: SearchResults }) => object[]
Optional
Receives the items and is called before displaying them. Should return a new array with the same shape as the original array. Useful for transforming, removing, or reordering items. In addition, the full If you’re transforming an attribute you’re using with the |
||
Copy
|
|||
classNames
|
type: Partial<HitsClassNames>
Optional
CSS classes to pass to the widget’s elements. This is useful to style widgets with class-based CSS frameworks like Bootstrap or Tailwind CSS.
|
||
Copy
|
|||
...props
|
type: React.ComponentProps<'div'>
Optional
Any |
||
Copy
|
Hook
React InstantSearch Hooks let you create your own UI for the <Hits>
widget with useHits()
. Hooks provide APIs to access the widget state and interact with InstantSearch.
The useHits()
Hook accepts parameters and returns APIs.
Usage
First, create your React component:
import { useHits } from 'react-instantsearch-hooks-web';
function CustomHits(props) {
const { hits, results, sendEvent } = useHits(props);
return <>{/* Your JSX */}</>;
}
Then, render the widget:
<CustomHits {...props} />
Parameters
Hooks accept parameters. You can pass them manually, or forward the props from your custom component.
When you provide a function to Hooks, make sure to pass a stable reference to avoid rendering endlessly (for example, with useCallback()
). Objects and arrays are memoized; you don’t need to stabilize them.
escapeHTML
|
type: boolean
default: true
Whether to escape HTML tags from hits string values. |
||
Copy
|
|||
transformItems
|
type: (items: object[], metadata: { results: SearchResults }) => object[]
default: items => items
Receives the items and is called before displaying them. Should return a new array with the same shape as the original array. Useful for transforming, removing, or reordering items. In addition, the full If you’re transforming an attribute you’re using with the |
||
Copy
|
APIs
Hooks return APIs, such as state and functions. You can use them to build your UI and interact with React InstantSearch.
hits
|
type: THit[]
The matched hits returned from Algolia. You can leverage the highlighting feature of Algolia directly from the render function. Check the |
results
|
type: SearchResults<THit>
The complete response from Algolia. It contains the |
sendEvent
|
type: (eventType: string, hits: Hit | Hits, eventName?: string) => void
The function to send The |
Example
1
2
3
4
5
6
7
8
import React from 'react';
import { useHits } from 'react-instantsearch-hooks-web';
function CustomHits(props) {
const { hits } = useHits(props);
return <>{/* Your JSX */}</>;
}