Each InstantSearch widget owns its part of the search state. While this fits the majority of use cases, sometimes you might want to read the state or refine the search outside of the InstantSearch lifecycle. For example, you may want to let users refine on a specific category from a product page, and not just from a refinement list. InstantSearch.js lets you access the render state of each widget, which you can use to create custom widgets or refine the search outside the InstantSearch.js lifecycle.Documentation Index
Fetch the complete documentation index at: https://algolia.com/llms.txt
Use this file to discover all available pages before exploring further.
The
renderState property is available starting in InstantSearch.js v4.9.Refining a search from the outside
In InstantSearch, therefinementList widget controls refinements for a given attribute.
You can access its state from the renderState property.
All examples in this guide assume you’ve included InstantSearch.js in your web page from a CDN. If, instead, you’re using it with a package manager, adjust how you import InstantSearch.js and its widgets for more information.
JavaScript
connectRefinementList render options.
You can access the state of brand and use the refine method to programmatically set a refinement.
JavaScript
HTML
JavaScript
Accessing the state of other widgets
When refining on a brand, you might end up with no results at all. By default, thehits widget displays a “No results” message. You can customize it using the connectHits connector along with the refinementList render state.
First, remove the default template for empty hits from the hits widget so it doesn’t display anything when there are no hits.
JavaScript
HTML
JavaScript
JavaScript
connectHits connector,
you can access the render state of the brand refinementList.
This lets you check whether “Pear” is a selected value and display a button to remove it.
The refine method is a toggle function. When you call it with a value that’s already refined, it removes it from the search state, and vice versa.
If you’re not familiar with customizing widgets with connectors, please check the guide on customizing the complete UI of widgets.
Using the panel widget
Thepanel widget gives you easier access to the render state.
This is the basic implementation of a panel.
JavaScript
renderState of hierarchicalMenu for each attribute are available in the collapsed and hidden methods of widget.
JavaScript
options include the same state as you’d find under search.renderState[indexName].hierarchicalMenu['hierarchicalCategories.lvl0'].
For example, when there are no facets to display, you can hide or collapse the hierarchicalMenu:
JavaScript