<ToggleRefinement>
About this widget
<ToggleRefinement>
is a widget that provides an on/off filter based on an attribute value.
For example, you can use this widget to only display products that apply for free shipping, or recipes that are gluten-free.
Make sure to declare the provided attribute
as an attribute for faceting.
You can also create your own UI with
useToggleRefinement()
.
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, ToggleRefinement } from 'react-instantsearch-hooks-web';
const searchClient = algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey');
function App() {
return (
<InstantSearch indexName="instant_search" searchClient={searchClient}>
<ToggleRefinement attribute="free_shipping" />
</InstantSearch>
);
}
Props
attribute
|
type: string
Required
The name of the attribute in the records. |
||
Copy
|
|||
label
|
type: string
Optional
The label to display for the checkbox. |
||
Copy
|
|||
on
|
type: boolean|string|number
Optional
The value of the refinement to apply on the attribute when checked. |
||
Copy
|
|||
off
|
type: boolean|string|number
Optional
The value of the refinement to apply on the attribute when unchecked. |
||
Copy
|
|||
classNames
|
type: Partial<ToggleRefinementClassNames>
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 <ToggleRefinement>
widget with useToggleRefinement()
. Hooks provide APIs to access the widget state and interact with InstantSearch.
The useToggleRefinement()
Hook accepts parameters and returns APIs.
Usage
First, create your React component:
import { useToggleRefinement } from 'react-instantsearch-hooks-web';
function CustomToggleRefinement(props) {
const {
value,
canRefine,
refine,
sendEvent,
createURL,
} = useToggleRefinement(props);
return <>{/* Your JSX */}</>;
}
Then, render the widget:
<CustomToggleRefinement {...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
|
|||
on
|
type: boolean|string|number
Optional
The value of the refinement to apply on the attribute when checked. |
||
Copy
|
|||
off
|
type: boolean|string|number
Optional
The value of the refinement to apply on the attribute when unchecked. |
||
Copy
|
APIs
Hooks return APIs, such as state and functions. You can use them to build your UI and interact with React InstantSearch.
value
|
type: ToggleRefinementValue
The current refinement.
Copy
|
||
canRefine
|
type: boolean
Whether the search state can be refined. |
||
Copy
|
|||
refine
|
type: ({ isRefined: boolean }) => void
Updates to the next state by applying the toggle refinement. |
||
Copy
|
|||
sendEvent
|
type: (eventType: string, facetValue: string, eventName?: string) => void
A function to send |
||
Copy
|
|||
createURL
|
type: () => string
Generates a URL for the next state. |
||
Copy
|
Example
1
2
3
4
5
6
7
8
import React from 'react';
import { useToggleRefinement } from 'react-instantsearch-hooks-web';
function ToggleRefinement(props) {
const { value, refine } = useToggleRefinement(props);
return <>{/* Your JSX */}</>;
}