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.
uiState
that maps to one or more Algolia search parameters.
For example,
the SearchBox
controls the query
,
the RefinementList
interacts with facetFilters
, parameter.
When a widget is mounted or unmounted, this is reflected in the InstantSearch UI state and included in search requests.
Consider what happens in a mobile search interface with filters (RefinementList
): when a user clicks the Filters button, a dialog opens and displays the refinements.
In many component libraries, the Dialog
component mounts and unmounts its content when toggled. This is problematic when used with InstantSearch components.
For example, if you have a RefinementList
widget nested in the dialog:
- The widget wouldn’t be mounted on the first app load because the dialog box is closed.
- When the dialog box opens, the widget mounts, adding it to the InstantSearch state and triggering a new request, even before a refinement has been selected.
- When the dialog box closes, the widget unmounts, removing it from the InstantSearch state and losing all selected refinements.
- Enable
preserveSharedStateOnUnmount
- Keep the widget mounted but hidden
- Persist the state on unmount using a virtual widget
- Use the Hook in a parent component
Enable preserveSharedStateOnUnmount
Enabling preserveSharedStateOnUnmount
prevents a widget’s state from being cleared when it’s unmounted, as long as other widgets share the same refinements.
Enabling this option changes how dispose
is used in the InstantSearch lifecycle.
By default when a widget is removed, it provides a cleared version of the state that will be propagated throughout the other widgets.
This option is available from v7.2.0 and will be the only option from React InstantSearch v8.
Keep the widget mounted but hidden
The most straightforward way to retain a widget’s state and refinements is to avoid unmounting it. You can, for example, hide the content of the dialog with CSS:Dialog
component from Headless UI lets you turn off unmounting with the unmount
option.
If you can’t avoid unmounting, you can try persisting the state on unmount.
Persist the state on unmount
If you can’t prevent unmounting a widget, you can keep track of the InstantSearch UI state to preserve it and apply it back when the dialog unmounts.Filters
and VirtualFilters
.
Filters
renders RefinementList
and
RangeInput
for the brand
and price
attributes.
This component is nested in Dialog
(see App.jsx
), so the widgets are mounted and unmounted as users toggle the dialog.
To avoid losing applied filters, the VirtualFilters
uses useRefinementList
and useRange
which register themselves in InstantSearch for the same brand
and price
attributes as the widgets.
The component is “renderless”, users don’t interact with it, but it allows persisting the state for brand
and price
within InstantSearch even when the widgets are unmounted.