UI libraries / InstantSearch.js / Widgets
Signature
function middleware({ instantSearchInstance }) {
  return {
    onStateChange({ uiState }) {},
    subscribe() {},
    unsubscribe() {}
  }
}

const search = instantsearch({
  // ...
})

search.use(middleware);

About this widget

Middleware is a function that returns an object with onStateChange, subscribe, and unsubscribe functions.

The middleware function doesn’t perform any actions itself but allows you to inject functionality into InstantSearch.js:

Requirements

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
function middleware({ instantSearchInstance }) {
  return {
    onStateChange({ uiState }) {
      // Do something with `uiState` whenever the state changes.
    },
    subscribe() {
      // Do something when the InstantSearch instance starts.
    },
    unsubscribe() {
      // Do something when the InstantSearch instance is disposed of.
    }
  }
}

// Declare your instantSearch instance normally
const search = instantsearch({
  // ...
})

// Use your middleware function
search.use(middleware);

Options

Parameter Description
instantSearchInstance
type: object
Required

You have access to the instance of instantsearch() which lets you read values from the instance or call instance methods like addWidgets, setUiState, and refresh.

1
2
3
4
5
6
7
8
9
10
11
12
13
function middleware({ instantSearchInstance }) {
  return {
    onStateChange({ uiState }) {
      // ...
    },
    subscribe() {
      // ...
    },
    unsubscribe() {
      // ...
    }
  }
}

Hooks

Parameter Description
onStateChange
type: ({ uiState }) => void
Required

The function is called with uiState whenever the state changes.

1
2
3
4
5
6
7
8
9
10
11
function middleware({ instantSearchInstance }) {
  return {
    // ...
    onStateChange({ uiState }) {
      const indexName = '<your-index-name>';
      const { query } = uiState[indexName];
      const title = document.querySelector('<your-selector>');
      title.innerText = query ? `Query: ${query}` : `No query`;
    },
  }
}
subscribe
type: () => void
Required

This function is called when the InstantSearch instance starts (when search.start() is called). This is where you can add event listeners, subscribe to an API, and run any side effects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function middleware({ instantSearchInstance }) {
  let subscription;
  let listener;

  return {
    // ...
    subscribe() {
      subscription = someAPI.subscribe();

      listener = (event) => {
        // do something
      }
      document.querySelector('<your-selector>').addEventListener('click', listener);
    },
  }
}
unsubscribe
type: () => void
Required

The function is called when the InstantSearch instance is disposed of. You can clean up anything you initiated in the subscribe function.

1
2
3
4
5
6
7
8
9
10
11
12
13
function middleware({ instantSearchInstance }) {
  let subscription;
  let listener;

  return {
    // ...
    unsubscribe() {
      subscription.unsubscribe();

      document.querySelector('<your-selector>').removeEventListener('click', listener);
    },
  }
}
Did you find this page helpful?