UI libraries / InstantSearch.js / Widgets
Signature
configure(searchParameters: object);
Import
1
import { configure } from 'instantsearch.js/es/widgets';

About this widget

The configure widget lets you provide raw search parameters to the Algolia API without rendering anything.

Any props you add to this widget is forwarded to Algolia. For more information on the different parameters you can set, have a look at the search parameters API reference.

Examples

1
2
3
4
configure({
  hitsPerPage: 8,
  enablePersonalization: true,
});

Options

searchParameters
type: object
Required

A list of search parameters to enable when the widget mounts.

1
2
3
4
5
6
configure({
  hitsPerPage: 8,
  distinct: true,
  clickAnalytics: true,
  enablePersonalization: true,
});

HTML output

This widget has no HTML output.

Customize the UI with connectConfigure

If you want to create your own UI of the configure widget, you can use connectors.

To use connectConfigure, you can import it with the declaration relevant to how you installed InstantSearch.js.

1
import { connectConfigure } from 'instantsearch.js/es/connectors';

Then it’s a 3-step process:

// 1. Create a render function
const renderConfigure = (renderOptions, isFirstRender) => {
  // Rendering logic
};

// 2. Create the custom widget
const customConfigure = connectConfigure(
  renderConfigure
);

// 3. Instantiate
search.addWidgets([
  customConfigure({
    // instance params
  })
]);

Create a render function

This rendering function is called before the first search (init lifecycle step) and each time results come back from Algolia (render lifecycle step).

const renderConfigure = (renderOptions, isFirstRender) => {
  const {
    function refine,
    object widgetParams,
  } = renderOptions;

  if (isFirstRender) {
    // Do some initial rendering and bind events
  }

  // Render the widget
}

Rendering options

refine
type: function

Removes the provided searchParameters and applies the one provided to the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const renderConfigure = (renderOptions, isFirstRender) => {
  const { refine } = renderOptions;

  if (isFirstRender) {
    const button = document.createElement('button');
    button.textContent = 'Sets "hitsPerPage" to 4';

    button.addEventListener('click', () => {
      refine({ hitsPerPage: 4 });
    });

    document.querySelector('#configure').appendChild(button);
  }
};
widgetParams
type: object

All original widget options forwarded to the render function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const renderConfigure = (renderOptions, isFirstRender) => {
  const { widgetParams } = renderOptions;

  widgetParams.container.innerHTML = `
    <pre>${JSON.stringify(widgetParams.searchParameters, null, 2)}</pre>
  `;
};

// ...

search.addWidgets([
  customConfigure({
    container: document.querySelector('#configure'),
    searchParameters: {
      hitsPerPage: 8,
    },
  })
]);

Create and instantiate the custom widget

We first create custom widgets from our rendering function, then we instantiate them. When doing that, there are two types of parameters you can give:

  • Instance parameters: they are predefined parameters that you can use to configure the behavior of Algolia.
  • Your own parameters: to make the custom widget generic.

Both instance and custom parameters are available in connector.widgetParams, inside the renderFunction.

const customConfigure = connectConfigure(
  renderConfigure
);

search.addWidgets([
  customConfigure({
    searchParameters: object,
  })
]);

Instance options

searchParameters
type: object
Required

A list of search parameters to enable when this widget renders.

1
2
3
4
5
6
7
customConfigure({
  searchParameters: {
    hitsPerPage: 8,
    distinct: true,
    clickAnalytics: true,
  },
});

Full example

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Create the render function
const renderConfigure = (renderOptions, isFirstRender) => {
  const { refine, widgetParams } = renderOptions;

  if (isFirstRender) {
    const button = document.createElement('button');
    const pre = document.createElement('pre');

    button.addEventListener('click', () => {
      refine({
        hitsPerPage: widgetParams.searchParameters.hitsPerPage === 8 ? 4 : 8,
      });
    });

    widgetParams.container.appendChild(button);
    widgetParams.container.appendChild(pre);
  }

  widgetParams.container.querySelector(
    'button'
  ).textContent = `Sets "hitsPerPage" to ${
    widgetParams.searchParameters.hitsPerPage === 8 ? 4 : 8
  }`;

  widgetParams.container.querySelector('pre').innerHTML = JSON.stringify(
    widgetParams.searchParameters,
    null,
    2
  );
};

// Create the custom widget
const customConfigure = connectConfigure(
  renderConfigure,
  () => {}
);

// Instantiate the custom widget
search.addWidgets([
  customConfigure({
    container: document.querySelector('#configure'),
    searchParameters: {
      hitsPerPage: 8,
    },
  })
]);
Did you find this page helpful?