What is a widget?
A widget is an object that implements specific lifecycle methods to interact with the InstantSearch instance. Widgets can:- Read and modify search parameters
- Render UI based on search results
- Handle user interactions and update search state
- Manage their own internal state
Widget interface defined in /home/daytona/workspace/source/packages/instantsearch.js/src/types/widget.ts:337-348.
Widget lifecycle
Widgets go through several lifecycle phases:Initialization
The
init() method is called once before the first search. This is where you set up event listeners and render the initial UI.Search parameters
The
getWidgetSearchParameters() method is called to collect search parameters from all widgets before making a search request.Widget interface
The complete widget interface includes these methods (from/home/daytona/workspace/source/packages/instantsearch.js/src/types/widget.ts):
Type identification
Identifier for the widget type, typically in the format
'ais.widgetName'.Optional identifier for UI widgets (as opposed to connectors).
Lifecycle methods
Called once before the first search. Use this to:
- Set up event listeners
- Render initial UI
- Initialize internal state
InitOptions include:instantSearchInstance: The InstantSearch instanceparent: The parent index widgethelper: The Algolia helperstate: Current search parametersuiState: Current UI state
Called after each search response. Use this to update the UI with new results.The
RenderOptions include everything from InitOptions plus:results: Search results from AlgoliascopedResults: Results for all indices
Called when the widget is removed. Return updated search parameters with your widget’s parameters removed.
State management
Converts UI state to search parameters. Called before each search.
Converts search parameters to UI state. Used for routing and state synchronization.
Render state (optional)
Returns the complete render state for the index, including this widget’s state.
Returns just this widget’s render state.
Built-in widgets
InstantSearch provides many built-in widgets. Here are the most common ones (from/home/daytona/workspace/source/packages/instantsearch.js/src/types/widget.ts:99-137):
Search widgets
Search widgets
ais.searchBox- Search inputais.hits- Display search resultsais.infiniteHits- Infinite scrolling resultsais.pagination- Page navigationais.stats- Search statistics
Refinement widgets
Refinement widgets
ais.refinementList- Faceted filtersais.menu- Single-selection facetsais.hierarchicalMenu- Hierarchical categoriesais.rangeSlider- Numeric range filterais.ratingMenu- Rating filterais.toggleRefinement- Boolean filter
Navigation widgets
Navigation widgets
Configuration widgets
Configuration widgets
ais.configure- Set search parametersais.index- Multi-index searchais.dynamicWidgets- Dynamic facets
Recommendation widgets
Recommendation widgets
ais.relatedProducts- Related itemsais.frequentlyBoughtTogether- Product bundlesais.trendingItems- Trending contentais.lookingSimilar- Similar items
Using widgets
- JavaScript
- React
- Vue
Creating custom widgets
You can create custom widgets by implementing the widget interface:Widget search parameters
Widgets can modify search parameters through thegetWidgetSearchParameters method. Common parameters include:
query- Search queryfacets,disjunctiveFacets,hierarchicalFacets- Facet configurationfacetFilters,numericFilters- Active filtershitsPerPage- Results per pagepage- Current pagearoundLatLng,aroundRadius- Geo search
/home/daytona/workspace/source/packages/instantsearch.js/src/connectors/search-box/connectSearchBox.ts:169-171):
Widget dependencies
Widgets can depend on eithersearch or recommend APIs:
relatedProducts and frequentlyBoughtTogether use the recommend API.
Best practices
Keep widgets focused
Each widget should have a single responsibility. Use multiple widgets for complex UIs.
Clean up resources
Always implement
dispose() to remove event listeners and clean up DOM elements.Use connectors for reusability
Separate business logic with connectors when building custom widgets for multiple frameworks.
Leverage render state
Implement
getRenderState and getWidgetRenderState for framework integrations and debugging.Related resources
Connectors
Separate business logic from rendering
InstantSearch instance
Learn about the main instance
Search state
Understanding UI state management
Widget API reference
Complete widget documentation