<Menu>
<Menu attribute={string} // Optional parameters limit={number} showMore={boolean} showMoreLimit={number} sortBy={UseMenuProps['sortBy']} transformItems={UseMenuProps['transformItems']} classNames={Partial<MenuClassNames>} ...props={React.ComponentProps<'div'>} />
About this widget
<Menu>
is a widget that displays a list of facets and lets users choose a single value.
Requirements
Ensure that the attribute provided to the hook is already declared as an attribute for faceting.
You can also create your own UI with
useMenu()
.
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
import React from 'react';
import algoliasearch from 'algoliasearch/lite';
import { InstantSearch, Menu } from 'react-instantsearch-hooks-web';
const searchClient = algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey');
function App() {
return (
<InstantSearch indexName="instant_search" searchClient={searchClient}>
<Menu attribute="categories" />
</InstantSearch>
);
}
Props
attribute
|
type: string
Required
The name of the attribute in the records. |
||
Copy
|
|||
limit
|
type: number
default: 10
How many facet values to retrieve.
|
||
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 items to display if the widget is showing more than the |
||
Copy
|
|||
sortBy
|
type: UseMenuProps['sortBy']
default: ["isRefined", "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 If you leave the default setting for this parameter, and you’ve set |
||
Copy
|
|||
transformItems
|
type: UseMenuProps['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<MenuClassNames>
Optional
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'>
Optional
Any |
||
Copy
|
Hook
React InstantSearch Hooks let you create your own UI for the <Menu>
widget with useMenu()
. Hooks provide APIs to access the widget state and interact with InstantSearch.
The useMenu()
Hook accepts parameters and returns APIs.
Usage
First, create your React component:
import { useMenu } from 'react-instantsearch-hooks-web';
function CustomMenu(props) {
const {
items,
createURL,
refine,
canRefine,
isShowingMore,
toggleShowMore,
canToggleShowMore,
sendEvent,
} = useMenu(props);
return <>{/* Your JSX */}</>;
}
Then, render the widget:
<CustomMenu {...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.
attribute
|
type: string
Required
The name of the attribute in the records. |
||
Copy
|
|||
limit
|
type: number
default: 10
How many facet values to retrieve.
|
||
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 items to display if the widget is showing more than the limit items. |
||
Copy
|
|||
sortBy
|
type: UseMenuProps['sortBy']
default: ["isRefined", "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 If you leave the default setting for this parameter, and you’ve set |
||
Copy
|
|||
transformItems
|
type: UseMenuProps['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: MenuItem[]
The elements that can be refined for the current search results.
Copy
|
||
createURL
|
type: (value: string) => string
Creates the next state URL of a selected refinement. |
||
refine
|
type: (value: string) => string
Applies the selected refinement. |
||
canRefine
|
type: boolean
Whether a refinement can be applied. |
||
isShowingMore
|
type: boolean
Whether the menu is displaying all the menu items. |
||
toggleShowMore
|
type: () => void
Toggles the number of values displayed between |
||
canToggleShowMore
|
type: boolean
Whether the Show more button can be activated, meaning there are enough additional items to display, or already displaying over the |
||
sendEvent
|
type: (eventType: string, facetValue: string, eventName?: string) => void
Sends an event to the Insights middleware. |
Example
1
2
3
4
5
6
7
8
import React from 'react';
import { useMenu } from 'react-instantsearch-hooks-web';
function CustomMenu(props) {
const { items, refine } = useMenu(props);
return <>{/* Your JSX */}</>;
}