middleware
function middleware({ instantSearchInstance }) { return { onStateChange({ uiState }) {}, subscribe() {}, unsubscribe() {} } }
About this widget
Middleware is a function returning an object with onStateChange
, subscribe
and unsubscribe
functions. With the middleware API, you can inject functionalities in the InstantSearch.js lifecycle.
Requirements
- Use InstantSearch.js v4.8.3 or later.
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function middleware({ instantSearchInstance }) {
return {
onStateChange({ uiState }) {
// Do something with `uiState` every time the state changes.
},
subscribe() {
// Do something when the InstantSearch instance starts.
},
unsubscribe() {
// Do something when the InstantSearch instance is disposed.
}
}
}
function Middleware() {
const { use } = useInstantSearch();
React.useLayoutEffect(() => {
return use(middleware);
});
}
function App() {
return (
<InstantSearch
searchClient={searchClient}
indexName="instant_search"
>
<Middleware />
</InstantSearch>
);
}
Options
instantSearchInstance
|
type: object
Required
You have access to the instance of |
||
Copy
|
Hooks
onStateChange
|
type: ({ uiState }) => void
Required
The function called with |
||
Copy
|
|||
subscribe
|
type: () => void
Required
The function called when the InstantSearch instance starts, i.e., when |
||
Copy
|
|||
unsubscribe
|
type: () => void
Required
The function called when the InstantSearch instance is disposed. You can clean up anything you initiated in the |
||
Copy
|