useQueryRules()
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 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: 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 |
||
Copy
|
Returns
items
|
type: any[]
The items that matched the Rule. |
||
Copy
|