Migrate from React InstantSearch v6 to React InstantSearch v7
React InstantSearch v7 is based on InstantSearch.js and lets you fully control the rendering. With React InstantSearch v7, you can create your own UI components using React Hooks.This guide helps you migrate from React InstantSearch v6 to React InstantSearch v7.
If you’re using older versions of React InstantSearch, upgrade to v6 first.
Packages
React InstantSearch v7 is available as two packages:react-instantsearch: it provides Hooks, DOM-based components and server utilities. This is the recommended package to use if you are building a web app.react-instantsearch-core: the core package with all non-DOM APIs such as Hooks, server utilities, and theInstantSearchcomponent. All these APIs are re-exported inreact-instantsearch. You should use this package if you are building a React Native app, or writing your own UI components.
react-instantsearch-dom: usereact-instantsearchinstead.react-instantsearch-native: usereact-instantsearch-coreinstead.
Codemods
React InstantSearch provides a codemod to automatically update your code and use the new package names and APIs from React InstantSearch v7. What the codemod does:- Replace
react-instantsearch-domimports withreact-instantsearch. - Update most prop names and their values.
- Add
TODOcomments for the parts that need manual changes, such as replacing thedefaultRefinementprops, and someInstantSearchprops. - Update all translations keys to their new names.
- Replace connectors with their corresponding Hook.
- Replace
react-instantsearch-nativeimports. There’s no direct equivalent in React InstantSearch v7. You need to update your code manually usingreact-instantsearch-core. - Update custom components.
- Update types for TypeScript users.
src/ folder with your React InstantSearch v6 code, you can run the codemod with the following command:
TODO (Codemod generated) in your codebase, which will point you to the right sections of this guide.
Codemods might output code that doesn’t follow your coding style. Make sure to run your code formatter before committing the changes.
Components
React InstantSearch v7 provides most of the same UI components as React InstantSearch. When no UI component is available, you can use the matching Hooks to build it yourself.| React InstantSearch v6 | React InstantSearch v7 |
|---|---|
Breadcrumb | Breadcrumb Prop changes (see step-by-step) |
ClearRefinements | ClearRefinements Prop changes (see step-by-step) |
Configure | Configure No changes |
CurrentRefinements | CurrentRefinements Prop changes (see step-by-step) |
DynamicWidgets | DynamicWidgets No changes |
ExperimentalConfigureRelatedItems | RelatedProducts No UI component |
HierarchicalMenu | HierarchicalMenu Prop changes (see step-by-step) |
Highlight | Highlight Prop changes (see step-by-step) |
Hits | Hits No changes |
HitsPerPage | HitsPerPage Prop changes (see step-by-step) |
Index | Index No changes |
InfiniteHits | InfiniteHits Prop changes (see step-by-step) |
InstantSearch | InstantSearch Prop changes (see step-by-step) |
Menu | Menu Prop changes (see step-by-step) |
MenuSelect | useMenu No UI component |
NumericMenu | useConnector(connectNumericMenu) No UI component |
Pagination | Pagination Prop changes (see step-by-step) |
Panel | No equivalent available |
PoweredBy | PoweredBy Prop changes (see step-by-step) |
QueryRuleContext | useQueryRules No UI component |
QueryRuleCustomData | useQueryRules No UI component |
RangeInput | RangeInput Prop changes (see step-by-step) |
RangeSlider | useRange No UI component |
RatingMenu | useMenu No UI component |
RefinementList | RefinementList Prop changes (see step-by-step) |
RelevantSort | useConnector(connectRelevantSort) No UI component |
ScrollTo | No equivalent available (read more) |
SearchBox | SearchBox Prop changes (see step-by-step) |
Snippet | Snippet Prop changes (see step-by-step) |
SortBy | SortBy Prop changes (see step-by-step) |
Stats | Stats Prop changes (see step-by-step) |
ToggleRefinement | ToggleRefinement Prop changes (see step-by-step) |
VoiceSearch | useConnector(connectVoiceSearch) No UI component |
Breadcrumb
Replace rootURL with rootPath
TheBreadcrumb component now takes an optional rootPath prop which replaces rootURL.
React
Replace translations
The translations keys have changed.
React
ClearRefinements
Replace clearsQuery with includedAttributes or excludedAttributes
The ClearRefinements component now takes optional includedAttributes and excludedAttributes prop which replace clearsQuery.
By default, the component ignores the query parameter.
If clearsQuery is set to false in your app (or not set at all), you can remove it.
React
clearsQuery is set to true in your app, you can remove query from excludedAttributes.
React
query to includedAttributes if you’re already hand-picking what attributes to clear. Keep in mind that when you specify includedAttributes, only those attributes get cleared.
React
The
includedAttributes and excludedAttributes props can’t be used together.Replace translations
The translations keys have changed.
React
CurrentRefinements
Replace clearsQuery with includedAttributes or excludedAttributes
TheCurrentRefinements component now takes optional includedAttributes and excludedAttributes prop which replace clearsQuery.
By default, the component ignores the query parameter. If clearsQuery is set to true in your app, you can remove it.
React
clearsQuery is set to false in your app (or not set at all), you can remove query from excludedAttributes.
React
query to includedAttributes if you’re already hand-picking what attributes to clear. Keep in mind that when you specify includedAttributes, only those attributes get cleared.
React
The
includedAttributes and excludedAttributes props can’t be used together.HierarchicalMenu
Replace defaultRefinement with initialUiState on InstantSearch
The HierarchicalMenu component no longer accepts a defaultRefinement prop.
Instead, specify an initial UI state on your InstantSearch component.
Replace YourIndexName with the name of your Algolia index.
React
Replace facetOrdering with sortBy
By default, the component sorts categories with the sortBy prop using the rules of renderingContent.facetOrdering when set, and falls back on ascending name ("name:asc").
If facetOrdering is set to true in your app, you can remove it.
React
facetOrdering is set to false in your app,
you can set the sortBy prop with the sorting annotation of your choice,
or using a custom sorting function.
React
Replace translations
Thetranslations keys have changed.
React
Highlight
Replace tagName with highlightedTagName
TheHighlight component now takes an optional highlightedTagName prop which replaces tagName.
React
"em" to "mark" for highlightedTagName (formerly tagName). If tagName is to "mark" in your app, you can remove it.
React
tagName, you should now set it explicitly.
React
HitsPerPage
Replace defaultRefinement with default in items
TheHitsPerPage component no longer takes a required defaultRefinement prop. Instead, you can specify which item is selected by default by specifying a boolean default property in the items prop.
React
InfiniteHits
Import createInfiniteHitsSessionStorageCache from instantsearch.js
The built-insessionStorage implementation for the cache prop are now available from the instantsearch.js package.
React
Replace translations
Thetranslations keys have changed.
React
InstantSearch
Replace searchState with initialUiState
TheInstantSearch component now takes optional initialUiState prop which replaces searchState.
Replace YourIndexName with the name of your Algolia index.
React
To provide an initial
uiState, you must add the corresponding widgets to your implementation. In the previous example, you need to mount a SearchBox component and a HitsPerPage component (either built-in or virtual) for applying state to queries.initialUiState under the index name it applies to, even if your implementation targets a single index. If you’re doing multi-index, each piece of state must be nested under its own index. To share state between indices, you must repeat it.
React
Replace onSearchStateChange with onStateChange
If you were usingonSearchStateChange to control the instance and react to state changes, you can replace it with onStateChange. Using this prop makes you responsible for updating the state, using the exposed uiState and its setter setUiState.
React
Replace refresh prop with refresh from useInstantSearch()
Therefresh prop was removed. If you need to manually update the cache and refresh the frontend, you can replace the refresh with an imperative call to refresh from the useInstantSearch() Hook.
React
Replace resultsState , findResultsState and onSearchParameters with the new server-side rendering APIs
The server APIs have been simplified. If you were usingresultsState, findResultsState and onSearchParameters for server-side rendering, check the server-side rendering section to migrate.
If you were using
onSearchParameters for another use case than server-side rendering, please reach out.Move createURL in routing
If you were usingcreateURL to manipulate the URL, you can move it in the routing prop.
React
Menu
Replace defaultRefinement with initialUiState on InstantSearch
The Menu component no longer accepts a defaultRefinement prop. Instead, you can specify an initial UI state on your InstantSearch component.
JavaScript
Replace facetOrdering with sortBy
By default, the component sorts categories with thesortBy prop using the rules of renderingContent.facetOrdering when set, and falls back on refined item and ascending name (["isRefined", "name:asc"]).
If facetOrdering is set to true in your app, you can remove it.
React
facetOrdering is set to false in your app, you can set the sortBy prop with the sorting annotation of your choice, or using a custom sorting function.
React
Replace searchable with a custom implementation
TheMenu widget isn’t searchable. You can create a searchable version by using a custom widget with the useRefinementList() connector.
React
Replace translations
Thetranslations keys have changed.
React
Pagination
Replace defaultRefinement with initialUiState on InstantSearch
The Pagination component no longer accepts a defaultRefinement prop. Instead, you can specify an initial UI state on your InstantSearch component.
JavaScript
Replace translations
Thetranslations keys have changed.
React
PoweredBy
Replace translations with a custom implementation
The Algolia logo is now a single image, including the “Search by”, therefore thetranslations prop was removed. If you need to translate the image, you can use a custom widget with the usePoweredBy() connector.
React
RangeInput
Replace defaultRefinement with initialUiState on InstantSearch
The RangeInput component no longer accepts a defaultRefinement prop. Instead, you can specify an initial UI state on your InstantSearch component.
JavaScript
Replace translations
Thetranslations keys have changed.
React
RefinementList
Replace defaultRefinement with initialUiState on InstantSearch
The RefinementList component no longer accepts a defaultRefinement prop. Instead, you can specify an initial UI state on your InstantSearch component.
JavaScript
Replace facetOrdering with sortBy
By default, the component sorts categories with thesortBy prop using the rules of renderingContent.facetOrdering when set, and falls back on refined item, descending count, and ascending name (["isRefined", "count:desc", "name:asc"]).
If facetOrdering is set to true in your app, you can remove it.
React
facetOrdering is set to false in your app, you can set the sortBy prop with the sorting annotation of your choice, or using a custom sorting function.
React
Replace translations
Thetranslations keys have changed. Additionally, the placeholder translation now has its own top-level prop, searchablePlaceholder.
React
ScrollTo
The component provided a way to scroll to the top of search results each time the uiState changed (due to a page change for example).
This can be done in React InstantSearch v7 by using the useEffect React Hook or by adding an InstantSearch middleware through useInstantSearch(), in combination with scrollIntoView.
You can see an implementation in the ecommerce example.
SearchBox
Replace defaultRefinement with initialUiState on InstantSearch
The RefinementList component no longer accepts a defaultRefinement prop. Instead, you can specify an initial UI state on your InstantSearch component.
JavaScript
Replace showLoadingIndicator with custom CSS
The widget now always shows a loading indicator when the search is stalled. IfshowLoadingIndicator is set to false in your app (or not set at all), you can hide the loading indicator with CSS.
CSS
Replace submit with submitIconComponent , reset with resetIconComponent , and loadingIndicator with loadingIconComponent
To customize the submit, reset, and loading icons, you can use thesubmitIconComponent, resetIconComponent, and loadingIconComponent props. Unlike submit, reset, and loadingIndicator, these new props take React components. They give you access to the default and passed classNames.
React
Replace focusShortcuts with custom code
ThefocusShortcuts prop was removed. If you want to focus the search box with custom keyboard shortcuts, you can set it up yourself with custom code.
For example, you can use react-use with the useKey Hook.
React
Replace translations.placeholder with placeholder
Theplaceholder translation now has its own top-level prop, placeholder.
React
Snippet
Replace tagName with highlightedTagName
The Highlight component now takes an optional highlightedTagName prop which replaces tagName.
React
"em" to "mark" for highlightedTagName (formerly tagName). If tagName is to "mark" in your app, you can remove it.
React
tagName, you should now set it explicitly.
React
SortBy
Replace defaultRefinement with initialUiState on InstantSearch
The SortBy component no longer accepts a defaultRefinement prop. Instead, you can specify an initial UI state on your InstantSearch component.
JavaScript
Stats
Replace translations
Thetranslations key have changed.
React
ToggleRefinement
Replace defaultRefinement with initialUiState on InstantSearch
The ToggleRefinement component no longer accepts a defaultRefinement prop. Instead, you can specify an initial UI state on your InstantSearch component.
JavaScript
Replace value with on and off
By default,ToggleRefinement uses respectively true and undefined for new optional props on and off. You can replace the value prop with on (and off, if needed).
React
Connectors
React InstantSearch v7 provides the same connectors as React InstantSearch v6 except theconnectStateResults connector, which isn’t available in React InstantSearch v7.
If you were using connectors with higher-order components (HOCs), you can migrate to Hooks.
JavaScript
connectStateResults
React InstantSearch v7 doesn’t have the connectStateResults connector.
You can use the useInstantSearch() hook instead.
Connectors and Hooks
React InstantSearch v7 is a bridge between InstantSearch.js connectors and React Hooks. The connectors from React InstantSearch and InstantSearch.js were historically different APIs, meaning there are differences between the props that React InstantSearch connectors accept and the new Hooks. The React InstantSearch v7 package uses TypeScript natively. If your editor supports code completion (IntelliSense), you can use it to discover the new props.Higher-Order Components (HOCs)
React InstantSearch v7 doesn’t come with Higher Order Components (HOCs). However, you can create them by wrapping existing Hooks into simple HOCs:React
useConnector() Hook to create a connector.
React
Creating connectors
The connector API has changed to use InstantSearch.js connectors. The previouscreateConnector() function is no longer available.
React InstantSearch v7 works with all InstantSearch.js connectors—official Algolia connectors, and community ones.
To create your own Hook, you can use an existing connector or create your InstantSearch.js connector.
For more information, see Custom Hooks.
Routing
Routing now follows the InstantSearch.js routing APIs with theInstantSearch routing prop.
For more information, see:
Server-side rendering (SSR)
The server APIs have been simplified in React InstantSearch v7. ReplacefindResultsState() with getServerState().
This new API accepts an element <App />. You can pass props directly to <App />.
In your server code, you don’t need to provide your index name and your search client anymore, because they’re already in your App.
server.js
InstantSearch to support SSR,
only wrap the component to render on the server with InstantSearchSSRProvider.
Then, pass the server state by spreading the getServerState() prop.
App.js
Types
Types are now available in React InstantSearch v7. You can uninstall all InstantSearch types coming from DefinitelyTyped.Migrate from React InstantSearch Hooks to React InstantSearch v7
From v7, React InstantSearch Hooks is called React InstantSearch. The migration is straightforward as the APIs are the same. Only package names have changed, and several APIs have been deprecated.Package names
| Before | After |
|---|---|
react-instantsearch-hooks | react-instantsearch-core |
react-instantsearch-hooks-web | react-instantsearch |
react-instantsearch-hooks-server | react-instantsearch |
react-instantsearch-hooks-router-nextjs | react-instantsearch-router-nextjs |
package.json file:
package.json
package.json file, run npm install to install the updated dependencies.
You can then change your imports:
JavaScript
The
react-instantsearch-hooks-server functions are now included in react-instantsearch.Codemods
To automatically update your code to use the new package names and APIs from React InstantSearch v7, Algolia provides a codemod. First, uninstall thereact-instantsearch-hooks-* packages and install their react-instantsearch-* counterparts.
src/ folder, run the following command:
use to addMiddlewares
useInstantSearch’s use function has been renamed to addMiddlewares.
JavaScript
getServerState
The getServerState function now requires renderToString from react-dom/server to be passed as a second argument.
JavaScript
Stats
The translations prop of the Stats widget has changed and now accepts a rootElementText function instead of a stats function.
React
Upgrade event tracking
Starting from v6.43.0, React InstantSearch Hooks simplifies the event tracking process with theinsights option.
You no longer need to install the search-insights library or set up the insights middleware yourself.
Here are some benefits when using the insights option:
- It better handles the
userToken. Once you set it, all the search and event tracking calls include the token. - It automatically sends default events from built-in widgets such as
RefinementList,Menu, etc. You can also change the event payloads, or remove them altogether. - It lets you send custom events from your custom widgets.
- It simplifies forwarding events to third-party trackers.
search-insights or with the insights middleware, you should:
- Upgrade
react-instantsearch-hooksto v6.43.0 or greater - Migrate from using the
insightsmiddleware to theinsightsoption - Either update or remove the
search-insightslibrary
Use the insights option
Starting from v6.43.0, InstantSearch lets you enable event tracking with the insights option. You no longer need to set up the insights middleware yourself.
React
insights middleware in your code, you can now remove it and move its configuration to the insights option.
React
Update search-insights
Starting from v4.55.0, InstantSearch can load search-insights for you so the insightsClient option is no longer required.
If you prefer loading it yourself, make sure to update search-insights to at least v2.4.0 and forward the reference to insights.
If you’re using the UMD bundle with a script tag,
make sure to update the full code snippet (not just the version):
HTML
Remove search-insights
Starting from v4.55.0, InstantSearch loads search-insights for you from jsDelivr if not detected in the page. If you’ve installed search-insights, you can now remove it.
If you’re using the UMD bundle with a script tag, you can remove the snippet:
HTML
search-insights from your code:
InstantSearch loads
search-insights from the jsDelivr CDN, which requires that your site or app allows script execution from foreign resources. Check the security best practices for recommendations.Upgrade to Recommend
TheRelatedProducts widget is now available instead of the previous experimental widget EXPERIMENTAL_RelatedItems. To migrate, you change the widget name and update the props:
React