This is the React InstantSearch v7 documentation.
If you’re upgrading from v6, see the upgrade guide.
If you were using React InstantSearch Hooks,
this v7 documentation applies—just check for necessary changes.
To continue using v6, you can find the archived documentation.
Signature
Import
JavaScript
About this widget
The<HierarchicalMenu>
widget displays a hierarchical navigation menu, based on facet attributes.
To create a hierarchical menu:
- Decide on an appropriate facet hierarchy
- Determine your
attributes
for faceting from the dashboard or with an API client - Display the UI with the hierarchical menu widget.
You can also create your own UI with
useHierarchicalMenu
.Requirements
The objects to use in the hierarchical menu must follow this structure:JSON
JSON
>
(with spaces), but you can use a different one by using the separator option.
By default, the count of the refined root level is updated to match the count of the actively refined parent level.
Keep the root level count intact by setting
persistHierarchicalRootCount
in <InstantSearch>
.Examples
JavaScript
Props
The name of the attributes to generate the menu with.
To avoid unexpected behavior, you can’t use the same
attribute
prop in a different type of widget.JavaScript
The number of 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.JavaScript
Whether to display a button that expands the number of items.
JavaScript
The maximum number of displayed items (only used when
showMore
is set to true
).JavaScript
The level separator used in the records.
JavaScript
The prefix path to use if the first level isn’t the root level.Make sure to also include the root path in your UI state,
for example, by setting
initialUiState
or calling setUiState
.JavaScript
Whether to show the siblings of the selected parent level of the current refined value.This option doesn’t impact the root level. All root items are always visible.
JavaScript
sortBy
string[] | (a: FacetValue, b: FacetValue) => number
default:"['name:asc'], or `facetOrdering` if set"
How to sort refinements. Must be one or more of the following strings:
"count"
(same as"count:desc"
)"count:asc"
"count:desc"
"name"
(same as"name:asc"
)"name:asc"
"name:desc"
"isRefined"
(same as"isRefined:asc"
)"isRefined:asc"
"isRefined:desc"
Array.sort
.A function that receives the list of items before they are displayed.
It should return a new array with the same structure.
Use this to transform, filter, or reorder the items.The function also has access to the full
results
data,
including all standard response parameters
and parameters from the helper,
such as disjunctiveFacetsRefinements
.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.
root
. The root element of the widget.noRefinementRoot
. The root element when there are no refinements.list
. The list element.item
. Each item element.selectedItem
. The selected item element.parentItem
. The parent item of the list.link
. The link of each item.selectedItemLink
. The link of each selected item element.label
. The label of each item.count
. The count of each item.showMore
. The “Show more” button.disabledShowMore
. The disabled “Show more” button.
JavaScript
A dictionary of translations to customize the UI text and support internationalization.
showMoreButtonText
. The text for the “Show more” button.
JavaScript
Any
<div>
prop to forward to the widget’s root element.JavaScript
Hook
React InstantSearch 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.
It must be used inside the <InstantSearch>
component.
Usage
First, create your React component:JavaScript
JavaScript
Parameters
Hooks accept parameters. You can either pass them manually or forward props from a custom component.When passing functions to Hooks, ensure stable references to prevent unnecessary re-renders.
Use
useCallback()
for memoization.
Arrays and objects are automatically memoized.The names of the attributes inside the records.To avoid unexpected behavior, you can’t use the same
attribute
prop in a different type of widget.JavaScript
The level separator used in the records.
JavaScript
The path to use if the first level isn’t the root level.Make sure to also include the root path in your UI state,
for example, by setting
initialUiState
or calling setUiState
.JavaScript
Whether to show the siblings of the selected parent level of the current refined value.
JavaScript
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.JavaScript
Whether to display a button that expands the number of items.
JavaScript
The maximum number of displayed items.
Only used when
showMore
is set to true
.JavaScript
sortBy
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:
"count"
(same as"count:desc"
)"count:asc"
"count:desc"
"name"
(same as"name:asc"
)"name:asc"
"name:desc"
"isRefined"
(same as"isRefined:asc"
)"isRefined:asc"
"isRefined:desc"
compareFunction
.When you don’t set this parameter, and you’ve set facetOrdering
for this facet in renderingContent
, facets are sorted using facetOrdering
and use the default order as a fallback.JavaScript
A function that receives the list of items before they are displayed.
It should return a new array with the same structure.
Use this to transform, filter, or reorder the items.The function also has access to the full
results
data,
including all standard response parameters
and parameters from the helper,
such as disjunctiveFacetsRefinements
.APIs
Hooks return APIs, such as state and functions. You can use them to build your UI and interact with React InstantSearch.The list of items the widget can display.
TypeScript
Whether the list is expanded.
Whether users can click the “Show more” button.
Indicates if you can refine the search state.
Sets the path of the hierarchical filter and triggers a new search.
The function to send
click
events.- The
view
event is automatically sent when the facets are rendered. - The
click
event is automatically sent whenrefine
is called. - You can learn more about the
insights
middleware. eventType: 'click'
facetValue: string
Toggles the number of displayed values between
limit
and showMoreLimit
.Generates a URL for the next state.