RelatedProducts
The RelatedProducts
component lets you render Related Products or Related Content.
Installation
The Recommend React package is available on the npm registry.
1
2
3
yarn add @algolia/recommend-react
# or
npm install @algolia/recommend-react
If you don’t want to use a package manager, you can use a standalone endpoint:
1
2
3
4
<script src="https://cdn.jsdelivr.net/npm/@algolia/recommend-react"></script>
<script>
const { FrequentlyBoughtTogether, RelatedProducts, TrendingItems, TrendingFacets } = window['@algolia/recommend-react'];
</script>
Usage
You can customize how to render each item by passing a custom component to the itemComponent
prop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { RelatedProducts } from '@algolia/recommend-react';
import recommend from '@algolia/recommend';
const recommendClient = recommend('YourApplicationID', 'YourSearchOnlyAPIKey');
const indexName = 'YOUR_INDEX_NAME';
function RelatedItem({ item }) {
return (
<pre>
<code>{JSON.stringify(item)}</code>
</pre>
);
}
function App({ currentObjectID }) {
// ...
return (
<RelatedProducts
recommendClient={recommendClient}
indexName={indexName}
objectIDs={[currentObjectID]}
itemComponent={RelatedItem}
/>
);
}
Horizontal slider view
You can also wrap the component within a custom view. For example, you can use the provided HorizontalSlider
UI component to display items as a scrollable slider.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { RelatedProducts } from '@algolia/recommend-react';
import { HorizontalSlider } from '@algolia/ui-components-horizontal-slider-react';
import recommend from '@algolia/recommend';
import '@algolia/ui-components-horizontal-slider-theme';
const recommendClient = recommend('YourApplicationID', 'YourSearchOnlyAPIKey');
const indexName = 'YOUR_INDEX_NAME';
function RelatedItem({ item }) {
return (
<pre>
<code>{JSON.stringify(item)}</code>
</pre>
);
}
function App({ currentObjectID }) {
// ...
return (
<RelatedProducts
recommendClient={recommendClient}
indexName={indexName}
objectIDs={[currentObjectID]}
itemComponent={RelatedItem}
view={HorizontalSlider}
/>
);
}
Parameters
itemComponent
|
type: ({ item }) => JSX.Element
Required
The component to display each item. |
||
headerComponent
|
type: (props: ComponentProps) => JSX.Element
The function to render a header for your items. The default implementation is:
Copy
|
||
fallbackComponent
|
type: () => JSX.Element
A fallback component to render when no recommendations are returned. |
||
view
|
type: ViewProps
The view component to render your items into. For example, you can use the The default implementation is:
Copy
|
||
environment
|
type: typeof window
default: window
The environment in which your application is running. This is useful if you’re using Recommend in a different context than |
classNames
|
type: RelatedProductsClassNames
The class names for the component.
Copy
|
||
translations
|
type: RelatedProductTranslations
The translations for the component.
Copy
|
This component also accepts all the props that useRelatedProducts
supports.
recommendClient
|
type: RecommendClient
Required
The initialized Algolia recommend client. |
indexName
|
type: string
Required
The name of the target index. |
objectIDs
|
type: string[]
Required
An array of |
maxRecommendations
|
type: number
The number of recommendations to retrieve. Depending on the available recommendations and the other request parameters, the actual number of hits may be lower than that.
If |
threshold
|
type: number
Required
Threshold for the recommendations confidence score (between 0 and 100). Only recommendations with a greater score are returned. |
fallbackParameters
|
type: Omit<SearchParameters, 'page' | 'hitsPerPage' | 'offset' | 'length'>
List of search parameters to send. Additional filters to use as fallback when there aren’t enough recommendations. |
queryParameters
|
type: Omit<SearchParameters, 'page' | 'hitsPerPage' | 'offset' | 'length'>
List of search parameters to send. |
transformItems
|
type: (items: Array<RecordWithObjectID<TItem>>) => items
A function to transform the retrieved items before passing them to the component. It’s useful to add or remove items, change them, or reorder them. |