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
<SearchBox>
is a widget to let users perform a text-based query.
The search box usually is the main entry point to start the search on an InstantSearch page.
You typically place it at the top of a search experience so that users can start searching right away.
You can also create your own UI with
useSearchBox
.Examples
JavaScript
Props
The placeholder text of the input.
JavaScript
Function called every time the query changes. It takes two parameters:
query
: The current query.search
: The function to trigger the search.
- Debounce searches to regulate requests.
- Programmatically alter the query before sending it to Algolia.
search
.
If you don’t call this function, no search is triggered to Algolia.Whether to update the search state in the middle of a composition session.
This is useful when users need to search using non-latin characters.
JavaScript
Whether to make a search on every change to the query.
If
false
, new searches are only triggered by clicking the search button or by pressing the Enter
key while focusing the search box.JavaScript
Whether the input should be autofocused.
JavaScript
A callback to run when submitting the form of the search box.
JavaScript
A component to replace the icon in the submit button.The component receives the passed
classNames
prop.JavaScript
A component to replace the icon in the reset button.The component receives the passed
classNames
prop.JavaScript
A component to replace the loading icon.The component receives the passed
classNames
prop.JavaScript
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.form
. The form element.input
. The input element.submit
. The submit button.reset
. The reset button.loadingIndicator
. The loading indicator element.submitIcon
. The submit icon.resetIcon
. The reset icon.loadingIcon
. The loading icon.
JavaScript
A dictionary of translations to customize the UI text and support internationalization.
submitButtonTitle
. The submit button’s title.resetButtonTitle
. The reset button’s title.
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<SearchBox>
widget with useSearchBox
.
Hooks provide APIs to access the widget state and interact with InstantSearch.
The useSearchBox
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.APIs
Hooks return APIs, such as state and functions. You can use them to build your UI and interact with React InstantSearch.The query from the last search.
Sets a new query and searches.
Clears the query and searches.
UseWhether the search results take more than a certain time to come back from Algolia servers.This can be configured onstatus
fromuseInstantSearch
instead.
<InstantSearch>
with the stalledSearchDelay
props which defaults to 200 ms.