UI libraries / React InstantSearch / Widgets

This is the React InstantSearch v7 documentation. React InstantSearch v7 is the latest version of React InstantSearch and the stable version of React InstantSearch Hooks.

If you were using React InstantSearch v6, you can upgrade to v7.

If you were using React InstantSearch Hooks, you can still use the React InstantSearch v7 documentation, but you should check the upgrade guide for necessary changes.

If you want to keep using React InstantSearch v6, you can find the archived documentation.

Signature
const queryRulesApi = useQueryRules({
  // Optional parameters
  trackedFilters: function
  transformRuleContexts: function
  transformItems: function
}
Import
1
import { useQueryRules } from 'react-instantsearch';

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
17
18
19
20
21
22
23
24
25
26
27
import React from 'react';
import { useQueryRules } from 'react-instantsearch';

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

  return null;
}

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

  return (
    <>
      {items.map(({ title, link, banner }) => (
        <section key={title}>
          <h2>{title}</h2>
          <a href={link}>
            <img src={banner} alt="" />
          </a>
        </section>
      ))}
    </>
  );
}

Parameters

Parameter Description
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

Parameter Description
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 v7