This is the React InstantSearch v7 documentation.
If you’re upgrading from v6, see the upgrade guide.
If you were using React InstantSearch Hooks,
this v7 documentation applies—just check for necessary changes.
To continue using v6, you can find the archived documentation.
Signature
Import
JavaScript
About this Hook
TheuseQueryRules
Hook lets you interact with Algolia Rules.
Rule context
You can use theuseQueryRules
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. TheuseQueryRules
Hook expose custom data from rules.
Examples
Parameters
trackedFilters
Record<string, (facetValues: Array<string | number | boolean>) => Array<string | number | boolean>>
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 so that they only consist of alphanumeric characters, hyphens, and underscores.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.
A function that receives the list of items before they are displayed.
It should return a new array with the same structure.
Use this to transform, filter, or reorder the items.The function also has access to the full
results
data,
including all standard response parameters
and parameters from the helper,
such as disjunctiveFacetsRefinements
.Returns
The items that matched the rule.
JavaScript