API Reference / React InstantSearch Hooks / useQueryRules()
Signature
const queryRulesApi = useQueryRules({
  // Optional parameters
  trackedFilters: function
  transformRuleContexts: function
  transformItems: function
}
Import
1
import { useQueryRules } from 'react-instantsearch-hooks-web';

About this Hook

The useQueryRules() Hook lets you interact with Algolia Rules.

Rule context

You can use the useQueryRules() Hook to apply ruleContexts based on filters to trigger context-dependent Rules.
You might want to customize the users’ experience based on the filters of the search—for example, when they’re visiting the “Mobile” category, or when they selected the “Thriller” genre, etc.

Rules offer a custom experience based on contexts. This Hook lets you map filters to their associated Rule contexts so you can trigger context-based Rules when applying refinements.

Rule custom data

Rules can return custom data, which is useful to display banners or recommendations depending on the current search parameters. The useQueryRules() Hook expose custom data from Rules.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import React from 'react';
import { useQueryRules } from 'react-instantsearch-hooks-web';

// Query rule context
function QueryRuleContext(props) {
  useQueryRules(props);

  return null;
}

// Query rule custom data
function QueryRuleCustomData(props) {
  const { items } = useQueryRules(props);

  return <>{/* Your JSX */}</>;
}

Parameters

trackedFilters
type: Record<string, (facetValues: Array<string | number | boolean>) => Array<string | number | boolean>>
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} (for example 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
7
8
9
10
11
12
const trackedFilters = {
  genre: () => ['Comedy', 'Thriller'], // this tracks two static genre values
  rating: values => values, // this tracks all the rating values
};

function QueryRules() {
  const queryRulesApi = useQueryRules({
    trackedFilters,
  });

  return <>{/* Your JSX */}</>;
}
transformRuleContexts
type: (ruleContexts: string[]) => string[]
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
7
8
9
10
11
12
13
const transformRuleContexts = (ruleContexts) => {
  return ruleContexts.map((ruleContext) =>
    ruleContext.replace('ais-', 'custom-')
  );
};

function QueryRules() {
  const queryRulesApi = useQueryRules({
    transformRuleContexts,
  });

  return <>{/* Your JSX */}</>;
}
transformItems
type: (items: object[], metadata: { results: SearchResults }) => object[]
Optional

Receives the items and is called before displaying them. Should return a new array with the same shape as the original array. Useful for transforming, removing, or reordering items.

In addition, the full results data is available, which includes all regular response parameters, as well as parameters from the helper (for example disjunctiveFacetsRefinements).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Using only items
const transformItems = (items) => {
  return items.filter(item => typeof item.banner !== 'undefined');
};

// Using items and results
const transformItems = (items, { results }) => {
  return items.map(item => ({
    ...item,
    visible: results.page === 0,
  }));
};

function QueryRules() {
  const queryRulesApi = useQueryRules({
    transformItems,
  });

  return <>{/* Your JSX */}</>;
}

Returns

items
type: any[]

The items that matched the Rule.

1
2
3
4
5
6
7
8
9
10
11
12
13
function QueryRuleCustomData(props) {
  const { items } = useQueryRules(props);

  return (
    <ul>
      {items.map((item, index) => (
        <li key={index}>
          {item.title}
        </li>
      ))}
    </ul>
  );
}
Did you find this page helpful?
React InstantSearch Hooks v6