useQueryRules()
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.
const queryRulesApi = useQueryRules({ // Optional parameters trackedFilters: function transformRuleContexts: function transformItems: function }
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 Values are escaped to only consist of alphanumeric characters, hyphens, and underscores. |
||
Copy
|
|||
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. |
||
Copy
|
|||
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 |
||
Copy
|
Returns
Parameter | Description | ||
---|---|---|---|
items
|
type: any[]
The items that matched the Rule. |
||
Copy
|