<RangeInput>
1
import { RangeInput } from 'react-instantsearch-hooks-web';
About this widget
<RangeInput>
is a widget to let users select a numeric range using minimum and maximum inputs.
You need to specify the attribute
prop in attributes for faceting, either on the dashboard or using attributesForFaceting
with the API. The attribute values must be numbers, not strings.
You can also create your own UI with
useRange()
.
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
import React from 'react';
import algoliasearch from 'algoliasearch/lite';
import { InstantSearch, RangeInput } from 'react-instantsearch-hooks-web';
const searchClient = algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey');
function App() {
return (
<InstantSearch indexName="instant_search" searchClient={searchClient}>
<RangeInput attribute="price" />
</InstantSearch>
);
}
Props
attribute
|
type: string
Required
The name of the attribute in the records. |
||
Copy
|
|||
min
|
type: number
The minimum value for the input. When not provided, the minimum value is automatically computed by Algolia from the data in the index. |
||
Copy
|
|||
max
|
type: number
The maximum value for the input. When not provided, the maximum value is automatically computed by Algolia from the data in the index. |
||
Copy
|
|||
precision
|
type: number
default: 0
The number of digits to use after the decimal point. |
||
Copy
|
|||
classNames
|
type: Partial<RangeInputClassNames>
CSS classes to pass to the widget’s elements. This is useful to style widgets with class-based CSS frameworks like Bootstrap or Tailwind CSS.
|
||
Copy
|
|||
...props
|
type: React.ComponentProps<'div'>
Any |
||
Copy
|
Hook
React InstantSearch Hooks let you create your own UI for the <RangeInput>
widget with useRange()
. Hooks provide APIs to access the widget state and interact with InstantSearch.
The useRange()
Hook accepts parameters and returns APIs.
Usage
First, create your React component:
import { useRange } from 'react-instantsearch-hooks-web';
function CustomRangeInput(props) {
const {
start,
range,
canRefine,
refine,
sendEvent,
} = useRange(props);
return <>{/* Your JSX */}</>;
}
Then, render the widget:
<CustomRangeInput {...props} />
Parameters
Hooks accept parameters. You can pass them manually, or forward the props from your custom component.
When you provide a function to Hooks, make sure to pass a stable reference to avoid rendering endlessly (for example, with useCallback()
). Objects and arrays are memoized; you don’t need to stabilize them.
attribute
|
type: string
Required
The name of the attribute in the records. |
||
Copy
|
|||
min
|
type: number
The minimum value for the input. When not provided, the minimum value is automatically computed by Algolia from the data in the index. |
||
Copy
|
|||
max
|
type: number
The maximum value for the input. When not provided, the maximum value is automatically computed by Algolia from the data in the index. |
||
Copy
|
|||
precision
|
type: number
default: 0
The number of digits to use after the decimal point. |
||
Copy
|
APIs
Hooks return APIs, such as state and functions. You can use them to build your UI and interact with React InstantSearch.
start
|
type: RangeBoundaries
The current value for the refinement, with
Copy
|
||
range
|
type: Range
The current available value for the range.
Copy
|
||
canRefine
|
type: boolean
Whether the user can further refine. |
||
refine
|
type: (rangeValue: RangeBoundaries) => void
Sets a range to filter the results on. Both values are optional, and default to the higher and lower bounds. You can use
Copy
|
||
sendEvent
|
type: (eventType: string, facetValue: string, eventName?: string) => void
Sends an event to the Insights middleware. |
Example
1
2
3
4
5
6
7
8
import React from 'react';
import { useRange } from 'react-instantsearch-hooks-web';
function RangeInput(props) {
const { start, range, canRefine, refine, sendEvent } = useRange(props);
return <>{/* Your JSX */}</>;
}