<ClearRefinements>
This is the React InstantSearch v7 documentation. React InstantSearch v7 is the latest version of React InstantSearch and the stable version of React InstantSearch Hooks.
If you were using React InstantSearch v6, you can upgrade to v7.
If you were using React InstantSearch Hooks, you can still use the React InstantSearch v7 documentation, but you should check the upgrade guide for necessary changes.
If you want to keep using React InstantSearch v6, you can find the archived documentation.
<ClearRefinements // Optional props includedAttributes={string[]} excludedAttributes={string[]} transformItems={function} classNames={object} translations={object} ...props={ComponentProps<'div'>} />
1
import { ClearRefinements } from 'react-instantsearch';
About this widget
<ClearRefinements>
is a widget that resets the active refinements of the search.
You can control which attributes are cleared with the props.
You can also create your own UI with
useClearRefinements()
.
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, ClearRefinements } from 'react-instantsearch';
const searchClient = algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey');
function App() {
return (
<InstantSearch indexName="instant_search" searchClient={searchClient}>
<ClearRefinements />
</InstantSearch>
);
}
Props
Parameter | Description | ||
---|---|---|---|
includedAttributes
|
type: string[]
default: []
The attributes to include in the refinements to clear (all by default). Can’t be used with |
||
Copy
|
|||
excludedAttributes
|
type: string[]
default: ["query"]
The attributes to exclude from the refinements to clear. Can’t be used with |
||
Copy
|
|||
transformItems
|
type: (items: object[], metadata: { results: SearchResults }) => object[]
Optional
Function which receives the items to clear, which is called before clearing them. Should return a new array with the same shape as the original array. Useful for filtering items. In addition, the full |
||
Copy
|
|||
classNames
|
type: Partial<ClearRefinementsClassNames>
Optional
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.
|
||
Copy
|
|||
translations
|
type: Partial<ClearRefinementsTranslations>
Optional
A dictionary of translations to customize the UI text and support internationalization.
|
||
Copy
|
|||
...props
|
type: React.ComponentProps<'div'>
Optional
Any |
||
Copy
|
Hook
React InstantSearch let you create your own UI for the <ClearRefinements>
widget with useClearRefinements()
. Hooks provide APIs to access the widget state and interact with InstantSearch.
The useClearRefinements()
Hook accepts parameters and returns APIs.
Usage
First, create your React component:
import { useClearRefinements } from 'react-instantsearch';
function CustomClearRefinements(props) {
const { refine, canRefine, createURL } = useClearRefinements(props);
return <>{/* Your JSX */}</>;
}
Then, render the widget:
<CustomClearRefinements {...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.
Parameter | Description | ||
---|---|---|---|
includedAttributes
|
type: string[]
default: []
The attributes to include in the refinements to clear (all by default). Can’t be used with |
||
Copy
|
|||
excludedAttributes
|
type: string[]
default: ["query"]
The attributes to exclude from the refinements to clear. Can’t be used with |
||
Copy
|
|||
transformItems
|
type: (items: string[], metadata: { results: SearchResults }) => string[]
Optional
Function which receives the items to clear, which is called before clearing them. Should return a new array with the same shape as the original array. Useful for filtering items. In addition, the full |
||
Copy
|
APIs
Hooks return APIs, such as state and functions. You can use them to build your UI and interact with React InstantSearch.
Parameter | Description |
---|---|
refine
|
type: () => void
Clears all the currently refined values and triggers a new search. |
canRefine
|
type: boolean
Indicates if search state can be refined. |
createURL
|
type: () => string
Generates a URL of the next state. |
Example
1
2
3
4
5
6
7
8
9
10
11
12
import React from 'react';
import { useClearRefinements } from 'react-instantsearch';
function CustomClearRefinements(props) {
const { canRefine, refine } = useClearRefinements(props);
return (
<button disabled={!canRefine} onClick={refine}>
Clear refinements
</button>
);
}