API Reference / React InstantSearch Hooks / useQueryRules()
Signature
useQueryRules({
  // Optional parameters
  trackedFilters: UseQueryRulesProps['trackedFilters'];
  transformRuleContexts: UseQueryRulesProps['transformRuleContexts'];
  transformItems: UseQueryRulesProps['transformItems'];
}: UseQueryRulesProps) => void

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
const queryRulesApi = useQueryRules({
  // ...
  trackedFilters: {
    genre: () => ['Comedy', 'Thriller'], // this tracks two static genre values
    rating: values => values, // this tracks all the rating values
  },
});
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
const queryRulesApi = useQueryRules({
  transformRuleContexts(ruleContexts) {
    return ruleContexts
      .map(ruleContext => ruleContext.replace('ais-', 'custom-'));
  },
});
transformItems
type: (items: any[]) => any[]
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
const queryRulesApi = useQueryRules({
  transformItems(items) {
    return items.filter(item => typeof item.banner !== 'undefined');
  },

  /* or, combined with results */
  transformItems(items, { results }) {
    return items.map(item => ({
      ...item,
      visible: results.page === 0,
    }));
  },
});

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