Breadcrumb
<Breadcrumb attributes={string[]} // Optional parameters separator={React.Node} rootURL={string} transformItems={function} translations={object} />
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 Breadcrumb
widget is a secondary navigation scheme that lets the user see where the current page is in relation to the facet’s hierarchy.
It reduces the number of actions a user needs to take to get to a higher-level page and improves the discoverability of the app or website’s sections and pages. It is commonly used for websites with lot of data, organized into categories with subcategories.
If you want to select a specific refinement for your Breadcrumb
component, you need to use a Virtual Hierarchical Menu and set its defaultRefinement
that is then used by the Breadcrumb
.
Requirements
The objects to use in the breadcrumb must follow this structure:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[
{
"objectID": "321432",
"name": "lemon",
"categories.lvl0": "products",
"categories.lvl1": "products > fruits"
},
{
"objectID": "8976987",
"name": "orange",
"categories.lvl0": "products",
"categories.lvl1": "products > fruits"
}
]
It’s also possible to provide more than one path for each level:
1
2
3
4
5
6
7
8
[
{
"objectID": "321432",
"name": "lemon",
"categories.lvl0": ["products", "goods"],
"categories.lvl1": ["products > fruits", "goods > to eat"]
}
]
The attributes provided to the widget must be in attributes for faceting, either on the dashboard or using attributesForFaceting
with the API. By default, the separator is >
(with spaces) but you can use a different one by using the separator option.
If there is also a HierarchicalMenu
on the page, it must follow the same configuration.
Examples
1
2
3
4
5
6
7
8
9
10
import { Breadcrumb } from 'react-instantsearch-dom';
<Breadcrumb
attributes={[
'categories.lvl0',
'categories.lvl1',
'categories.lvl2',
'categories.lvl3',
]}
/>
Props
attributes
|
type: string[]
Required
A list of attributes to use to generate the hierarchy of the widget. |
||
Copy
|
|||
separator
|
type: React.Node
default: >
Optional
The character used to separate the elements of the breadcrumb. |
||
Copy
|
|||
rootURL
|
type: string
Optional
The URL (used in the |
||
Copy
|
|||
transformItems
|
type: function
default: items => items
Optional
Modifies the items being displayed, for example, to filter or sort them. It takes items as argument and expects them back in return. |
||
Copy
|
|||
translations
|
type: object
Optional
A mapping of keys to translation values.
|
||
Copy
|
HTML output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<div class="ais-Breadcrumb">
<ul class="ais-Breadcrumb-list">
<li class="ais-Breadcrumb-item">
<a class="ais-Breadcrumb-link" href="#">Home</a>
</li>
<li class="ais-Breadcrumb-item">
<span class="ais-Breadcrumb-separator"> > </span>
<a class="ais-Breadcrumb-link" href="#">Cameras & Camcorders</a>
</li>
<li class="ais-Breadcrumb-item ais-Breadcrumb-item--selected">
<span class="ais-Breadcrumb-separator"> > </span>
Digital Cameras
</li>
</ul>
</div>
Customize the UI with connectBreadcrumb
If you want to create your own UI of the Breadcrumb
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 Breadcrumb = () => {
// return the DOM output
};
// 2. Connect the component using the connector
const CustomBreadcrumb = connectBreadcrumb(Breadcrumb);
// 3. Use your connected widget
<CustomBreadcrumb />
Create a React component
const Breadcrumb = ({
object[] items,
function refine,
function createURL,
}) => {
// return the DOM output
};
Provided Props
items
|
type: object[]
The list of items the widget can display, with each item:
|
||
Copy
|
|||
refine
|
type: function
Clears the refinements. |
||
Copy
|
|||
createURL
|
type: function
Generates a URL for the corresponding search state. |
||
Copy
|
Create and instantiate your connected widget
<CustomBreadcrumb
attributes={string[]}
// Optional parameters
transformItems={function}
/>
Exposed Props
attributes
|
type: string[]
Required
A list of attributes to use to generate the hierarchy of the widget. |
||
Copy
|
|||
transformItems
|
type: function
default: items => items
Optional
Modifies the items being displayed, for example, to filter or sort them. It takes items as argument and expects them back in return. |
||
Copy
|
If SEO is critical to your search page, your custom HTML markup needs to be parsable:
- use plain
<a>
tags withhref
attributes for search engines bots to follow them, - use semantic markup with structured data when relevant, and test it.
Refer to our SEO checklist for building SEO-ready search experiences.
Full example
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
31
32
33
import { connectBreadcrumb } from 'react-instantsearch-dom';
const Breadcrumb = ({ items, refine, createURL }) =>
Boolean(items.length) && (
<ul>
<li>
<a
href={createURL()}
onClick={event => {
event.preventDefault();
refine();
}}
>
Home
</a>
</li>
{items.map(item => (
<li key={item.value}>
<a
href={createURL(item.value)}
onClick={event => {
event.preventDefault();
refine(item.value);
}}
>
{item.label}
</a>
</li>
))}
</ul>
);
const CustomBreadcrumb = connectBreadcrumb(Breadcrumb);