<TrendingItems>
This is the React InstantSearch v7 documentation. React InstantSearch v7 is the latest version of React InstantSearch and the stable version of React InstantSearch Hooks.
If you were using React InstantSearch v6, you can upgrade to v7.
If you were using React InstantSearch Hooks, you can still use the React InstantSearch v7 documentation, but you should check the upgrade guide for necessary changes.
If you want to keep using React InstantSearch v6, you can find the archived documentation.
<TrendingItems // Optional props facetName={string} facetValue={string} itemComponent={function} headerComponent={function} emptyComponent={function} limit: {number}, threshold: {number}, queryParameters: {object}, fallbackParameters: {object}, escapeHTML={boolean} transformItems={function} classNames={object} translations={object} ...props={ComponentProps<'div'>} />
1
import { TrendingItems } from 'react-instantsearch';
About this widget
Use the <TrendingItems>
widget to display a list of trending items.
If you’re using the deprecated recommend-react
UI library, please refer to the API reference for the <TrendingItems>
component or useTrendingItems()
Hook.
You can also create your own UI with
useTrendingItems()
.
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import React from 'react';
import { liteClient as algoliasearch } from 'algoliasearch/lite';
import { InstantSearch, TrendingItems } from 'react-instantsearch';
const searchClient = algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey');
function Item({ item }) {
return JSON.stringify(item);
}
function App() {
return (
<InstantSearch indexName="instant_search" searchClient={searchClient}>
<TrendingItems
facetName="category"
facetValue="Book"
itemComponent={Item}
/>
</InstantSearch>
);
}
With Carousel layout
Display the <TrendingItems>
widget as a scrollable carousel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import React from 'react';
import { liteClient as algoliasearch } from 'algoliasearch/lite';
import { InstantSearch, TrendingItems, Carousel } from 'react-instantsearch';
const searchClient = algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey');
function Item({ item }) {
return JSON.stringify(item);
}
function App() {
return (
<InstantSearch indexName="instant_search" searchClient={searchClient}>
<TrendingItems
facetName="category"
facetValue="Book"
itemComponent={Item}
layoutComponent={Carousel}
/>
</InstantSearch>
);
}
Props
facetName
|
type: string
Optional
The facet attribute to get recommendations for. This parameter should be used along with |
||
Copy
|
|||
facetValue
|
type: string
Optional
The value for the targeted facet name. This parameter should be used along with |
||
Copy
|
|||
itemComponent
|
type: (props: { item: THit }) => JSX.Element
Optional
A component that renders each recommendation from the results. It receives an When not provided, the widget displays the recommendation as a JSON string. |
||
Copy
|
|||
headerComponent
|
type: (props: { classNames: string[], items: THit[], translations: object }) => JSX.Element
Optional
A component that renders a title header. It receives When not provided, the widget displays a default header. |
||
Copy
|
|||
emptyComponent
|
type: () => JSX.Element
Optional
A component that renders when there are no recommendations. When not provided, the widget displays a default message. |
||
Copy
|
|||
layoutComponent
|
since: v7.13.0
type: (props: RecommendLayoutProps) => JSX.Element
Optional
The layout component to use to wrap all items. React InstantSearch provides a provides a built-in
|
||
Copy
|
|||
limit
|
type: number
Optional
The number of recommendations to retrieve. Depending on the available recommendations and the other request parameters, the actual number of items may be lower than that.
If |
||
Copy
|
|||
threshold
|
type: number
Optional
The threshold for the recommendations confidence score (between 0 and 100). Only recommendations with a greater score are returned. |
||
Copy
|
|||
queryParameters
|
type: Omit<SearchParameters, "hitsPerPage" | "length" | "offset" | "page">
Optional
List of search parameters to send. |
||
Copy
|
|||
fallbackParameters
|
type: Omit<SearchParameters, "hitsPerPage" | "length" | "offset" | "page">
Optional
List of search parameters to send as additional filters when there aren’t enough recommendations. |
||
Copy
|
|||
escapeHTML
|
type: boolean
default: true
Optional
Whether to escape HTML tags from recommendations string values. |
||
Copy
|
|||
transformItems
|
type: (items: THit[], metadata: { results: SearchResults }) => THit[]
Optional
Receives the recommendations and is called before displaying them. It returns a new array with the same “shape” as the original. This is helpful when transforming or reordering items. The entire |
||
Copy
|
|||
classNames
|
type: Partial<TrendingItemsClassNames>
Optional
The CSS classes you can override and pass to the widget’s elements. It’s useful to style widgets with class-based CSS frameworks like Bootstrap or Tailwind CSS.
|
||
Copy
|
|||
translations
|
type: Partial<RecommendTranslations>
Optional
A mapping of keys to translation values.
|
||
Copy
|
|||
...props
|
type: React.ComponentProps<'div'>
Optional
Any |
||
Copy
|
Hook
React InstantSearch let you create your own UI for the <TrendingItems>
widget with useTrendingItems()
. Hooks provide APIs to access the widget state and interact with InstantSearch.
The useTrendingItems()
Hook accepts parameters and returns APIs.
Usage
First, create your React component:
import { useTrendingItems } from 'react-instantsearch';
function CustomTrendingItems(props) {
const { items } = useTrendingItems(props);
return <>{/* Your JSX */}</>;
}
Then, render the widget:
<CustomTrendingItems {...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.
facetName
|
type: string
Optional
The facet attribute to get recommendations for. This parameter should be used along with |
||
Copy
|
|||
facetValue
|
type: string
Optional
The value for the targeted facet name. This parameter should be used along with |
||
Copy
|
|||
limit
|
type: number
Optional
The number of recommendations to retrieve. Depending on the available recommendations and the other request parameters, the actual number of items may be lower than that.
If |
||
Copy
|
|||
threshold
|
type: number
Optional
The threshold for the recommendations confidence score (between 0 and 100). Only recommendations with a greater score are returned. |
||
Copy
|
|||
queryParameters
|
type: Omit<SearchParameters, "hitsPerPage" | "length" | "offset" | "page">
Optional
List of search parameters to send. |
||
Copy
|
|||
fallbackParameters
|
type: Omit<SearchParameters, "hitsPerPage" | "length" | "offset" | "page">
Optional
List of search parameters to send as additional filters when there aren’t enough recommendations. |
||
Copy
|
|||
escapeHTML
|
type: boolean
default: true
Optional
Whether to escape HTML tags from recommendations string values. |
||
Copy
|
|||
transformItems
|
type: (items: THit[], metadata: { results: SearchResults }) => THit[]
Optional
Receives the recommendations and is called before displaying them. It returns a new array with the same “shape” as the original. This is helpful when transforming or reordering items. The entire |
||
Copy
|
APIs
Hooks return APIs, such as state and functions. You can use them to build your UI and interact with React InstantSearch.
items
|
type: THit[]
The matched recommendations returned from Algolia. |
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import React from 'react';
import { useTrendingItems } from 'react-instantsearch';
function CustomTrendingItems(props) {
const { items } = useTrendingItems(props);
return (
<ol>
{items.map((item) => (
<li key={item.objectID}>
<div style={{ wordBreak: 'break-all' }}>
{JSON.stringify(item).slice(0, 100)}
</div>
</li>
))}
</ol>
);
}