API Reference / React InstantSearch Widgets / QueryRuleContext
Apr. 24, 2019

QueryRuleContext

Widget signature
<QueryRuleContext
  trackedFilters={object}
  // Optional parameters
  transformRuleContexts={function}
/>

About this widget

The QueryRuleContext widget lets you apply ruleContexts based on filters to trigger contextual Query Rules.

Query Rules offer a custom experience based on rule contexts. You might want to customize the users’ experience based on the filters of the search (e.g., they’re visiting the “Mobile” category, they selected the “Thriller” genre, etc.) This widget lets you map these filters with their associated rule contexts, so you can trigger contextual Query Rules upon refinement.

Examples

1
2
3
4
5
6
7
8
import { QueryRuleContext } from 'react-instantsearch-dom';

<QueryRuleContext
  trackedFilters={{
    genre: () => ['Comedy', 'Thriller'],
    rating: values => values,
  }}
/>

Props

trackedFilters
type: object
Required

The filters to track to trigger rule contexts.

Each filter is a function which name is the attribute you want to track. They receive their current refinements as arguments. You can either compute the filters you want to track based on those, or return static values. When the tracked values are refined, it toggles the associated rule contexts.

The added rule contexts follow the format ais-{attribute}-{value} (e.g. ais-genre-Thriller). If the context of your rule follows another format, you can specify it using the transformRuleContexts option.

Values are escaped to only consist of alphanumeric characters, hyphens, and underscores.

1
2
3
4
5
6
<QueryRuleContext
  trackedFilters={{
    genre: () => ['Comedy', 'Thriller'], // this tracks two static genre values,
    rating: values => values, // this tracks all the rating values
  }}
/>
transformRuleContexts
type: function
Optional

A function to apply to the rule contexts before sending them to Algolia. This is useful to rename rule contexts that follow a different naming convention.

1
2
3
4
5
6
<QueryRuleContext
  // ...
  transformRuleContexts={ruleContexts =>
    ruleContexts.map(ruleContext => ruleContext.replace('ais-', 'custom-'))
  }
/>

Customize the UI - connectQueryRules

If you want to create your own UI of the QueryRuleContext widget or use another UI library, you can use connectors.

Connectors are higher-order components. They encapsulate the logic for a specific kind of widget and they provide a way to interact with the InstantSearch context.

They have an outer component API that we call exposed props, and they provide some other props to the wrapped components which are called the provided props.

This connector is also used to build other widgets: QueryRuleCustomData

It’s a 3-step process:

// 1. Create a React component
const QueryRuleContext = () => {
  // return the DOM output
};

// 2. Connect the component using the connector
const CustomQueryRuleContext = connectQueryRules(QueryRuleContext);

// 3. Use your connected widget
<CustomQueryRuleContext />

Create a React component

const QueryRuleContext = () => {
  // return the DOM output
};

Provided Props

Create and instantiate your connected widget

const CustomQueryRuleContext = connectQueryRules(QueryRuleContext);

<CustomQueryRuleContext
  // Optional parameters
  trackedFilters={object}
  transformRuleContexts={function}
/>

Exposed Props

trackedFilters
type: object
Optional

The filters to track to trigger rule contexts.

Each filter is a function which name is the attribute you want to track. They receive their current refinements as arguments. You can either compute the filters you want to track based on those, or return static values. When the tracked values are refined, it toggles the associated rule contexts.

The added rule contexts follow the format ais-{attribute}-{value} (e.g. ais-genre-Thriller). If the context of your rule follows another format, you can specify it using the transformRuleContexts option.

Values are escaped to only consist of alphanumeric characters, hyphens, and underscores.

1
2
3
4
5
6
<CustomQueryRuleContext
  trackedFilters={{
    genre: () => ['Comedy', 'Thriller'], // this tracks two static genre values, 
    rating: values => values, // this tracks all the rating values
  }}
/>
transformRuleContexts
type: function
Optional

A function to apply to the rule contexts before sending them to Algolia. This is useful to rename rule contexts that follow a different naming convention.

1
2
3
4
5
6
<CustomQueryRuleContext
  // ...
  transformRuleContexts={ruleContexts =>
    ruleContexts.map(ruleContext => ruleContext.replace('ais-', 'custom-'))
  }
/>

Full example

1
2
3
4
5
import { connectQueryRules } from 'react-instantsearch-dom';

const QueryRuleContext = () => null;

const CustomQueryRuleContext = connectQueryRules(QueryRuleContext);

Did you find this page helpful?