<HierarchicalMenu>
<HierarchicalMenu attributes={string[]} // Optional props limit={number} showMore={boolean} showMoreLimit={number} separator={string} rootPath={string} showParentLevel={boolean} sortBy={string[] | Function} transformItems={(items: HierarchicalMenuItem[]) => HierarchicalMenuItem[]} classNames={Partial<HierarchicalMenuClassNames>} ...props={React.ComponentProps<'div'>} />
About this widget
<HierarchicalMenu>
is a widget that creates a navigation based on a hierarchy of facet attributes. It’s commonly used for categories with subcategories.
Requirements
The objects to use in the hierarchical menu 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.
You can also create your own UI with
useHierarchicalMenu()
.
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import React from 'react';
import algoliasearch from 'algoliasearch/lite';
import { InstantSearch, HierarchicalMenu } from 'react-instantsearch-hooks-web';
const searchClient = algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey');
function App() {
return (
<InstantSearch indexName="instant_search" searchClient={searchClient}>
<HierarchicalMenu
attributes={[
'hierarchicalCategories.lvl0',
'hierarchicalCategories.lvl1',
'hierarchicalCategories.lvl2',
'hierarchicalCategories.lvl3',
]}
/>
</InstantSearch>
);
}
Props
attributes
|
type: string[]
Required
The name of the attributes to generate the menu with. |
||
Copy
|
|||
limit
|
type: number
default: 10
How many facet values to retrieve. When you enable the showMore feature, this is the number of facet values to display before clicking the “Show more” button. |
||
Copy
|
|||
showMore
|
type: boolean
default: false
Whether to display a button that expands the number of items. |
||
Copy
|
|||
showMoreLimit
|
type: number
The maximum number of displayed items (only used when showMore is set to |
||
Copy
|
|||
separator
|
type: string
default: " > "
The level separator used in the records. |
||
Copy
|
|||
rootPath
|
type: string
The prefix path to use if the first level isn’t the root level. |
||
Copy
|
|||
showParentLevel
|
type: boolean
default: true
Whether to show the siblings of the selected parent level of the current refined value. |
||
Copy
|
|||
sortBy
|
type: string[] | (a: RefinementListItem, b: RefinementListItem) => number
default: Uses facetOrdering if set, ["name:asc"]
How to sort refinements. Must be one or more of the following strings:
It’s also possible to give a function, which receives items two by two, like JavaScript’s |
||
Copy
|
|||
transformItems
|
type: UseRefinementListProps['transformItems']
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 |
||
Copy
|
|||
classNames
|
type: Partial<HierarchicalMenuClassNames>
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'>
Any |
||
Copy
|
Hook
React InstantSearch Hooks let you create your own UI for the <HierarchicalMenu>
widget with useHierarchicalMenu()
. Hooks provide APIs to access the widget state and interact with InstantSearch.
The useHierarchicalMenu()
Hook accepts parameters and returns APIs.
Usage
First, create your React component:
import { useHierarchicalMenu } from 'react-instantsearch-hooks-web';
function CustomHierarchicalMenu(props) {
const {
items,
isShowingMore,
canToggleShowMore,
canRefine,
refine,
sendEvent,
toggleShowMore,
createURL,
} = useHierarchicalMenu(props);
return <>{/* Your JSX */}</>;
}
Then, render the widget:
<CustomHierarchicalMenu {...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 with useCallback()
to avoid rendering endlessly. Objects and arrays are memoized so you don’t have to stabilize them.
attributes
|
type: string[]
Required
The names of the attributes inside the records. |
||
Copy
|
|||
separator
|
type: string
default: " > "
The level separator used in the records. |
||
Copy
|
|||
rootPath
|
type: string
The path to use if the first level isn’t the root level. |
||
Copy
|
|||
showParentLevel
|
type: boolean
default: true
Whether to show the siblings of the selected parent level of the current refined value. |
||
Copy
|
|||
limit
|
type: number
default: 10
How many facet values to retrieve. When you enable the showMore feature, this is the number of facet values to display before clicking the Show more button. |
||
Copy
|
|||
showMore
|
type: boolean
default: false
Whether to display a button that expands the number of items. |
||
Copy
|
|||
showMoreLimit
|
type: number
The maximum number of displayed items. Only used when showMore is set to |
||
Copy
|
|||
sortBy
|
type: string[] | (a: RefinementListItem, b: RefinementListItem) => number
default: ["name:asc"] (or `facetOrdering` if set)
How to sort refinements. Must be one or more of the following strings:
You can also use a sort function that behaves like the standard Javascript When you don’t set this parameter, and you’ve set |
||
Copy
|
|||
transformItems
|
type: UseHierarchicalMenuProps['transformItems']
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 |
||
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: HierarchicalMenuItem[]
The list of items the widget can display. |
||
Copy
|
|||
isShowingMore
|
type: boolean
Whether the list is expanded. |
||
canToggleShowMore
|
type: boolean
Whether the “Show more” button can be clicked. |
||
canRefine
|
type: boolean
Indicates if search state can be refined. |
||
refine
|
type: (value: string) => void
Sets the path of the hierarchical filter and triggers a new search. |
||
sendEvent
|
type: (eventType: string, facetValue: string, eventName?: string) => void
The function to send
|
||
toggleShowMore
|
type: () => void
Toggles the number of displayed values between |
||
createURL
|
type: (value: string) => string
Generates a URL for the next state. |
Example
1
2
3
4
5
6
7
8
import React from 'react';
import { useHierarchicalMenu } from 'react-instantsearch-hooks-web';
function CustomHierarchicalMenu(props) {
const { items, refine } = useHierarchicalMenu(props);
return <>{/* Your JSX */}</>;
}