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
<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.
You can also create your own UI with
useToggleRefinement
.Requirements
Make sure to declare the providedattribute
as an attribute for faceting.
Examples
JavaScript
Props
The name of the attribute in the records.To avoid unexpected behavior, you can’t use the same
attribute
prop in a different type of widget.JavaScript
The label to display for the checkbox.
JavaScript
The value of the refinement to apply on the attribute when checked.
The value of the refinement to apply on the attribute when unchecked.
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.label
. The label element.checkbox
. The checkbox element.labelText
. The text element of the label.
JavaScript
Any
<div>
prop to forward to the root element of the widget.JavaScript
Hook
React InstantSearch 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.
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 name of the attribute in the records.To avoid unexpected behavior, you can’t use the same
attribute
prop in a different type of widget.JavaScript
The value of the refinement to apply on the attribute when checked.
The value of the refinement to apply on the attribute when unchecked.
APIs
Hooks return APIs, such as state and functions. You can use them to build your UI and interact with React InstantSearch.The current refinement.
TypeScript
Whether the search state can be refined.
JavaScript
Updates to the next state by applying the toggle refinement.
JavaScript
A function to send
click
events.
The click
event is automatically sent when calling refine
.
To learn more, see the insights
middleware.JavaScript
Generates a URL for the next state.
JavaScript