QueryTrigger (Search Engine)
QueryTrigger (Search Engine)
This is for:
DeveloperExample Implementation
query-trigger.fn.tsx
import {QueryTrigger as HeadlessQueryTrigger} from '@coveo/headless';
import {useEffect, useState, FunctionComponent} from 'react';
interface HeadlessQueryTriggerProps {
controller: HeadlessQueryTrigger;
}
export const QueryTrigger: FunctionComponent<HeadlessQueryTriggerProps> = (
props
) => {
const {controller} = props;
const [state, setState] = useState(controller.state);
useEffect(() => controller.subscribe(() => updateState()), []);
const updateState = () => {
setState(props.controller.state);
};
if (state.wasQueryModified) {
return (
<div>
The query changed from {state.originalQuery} to {state.newQuery}
</div>
);
}
return null;
};
// usage
/**
* ```tsx
* const controller = buildQueryTrigger(engine);
*
* <QueryTriggerFn controller={controller} />;
* ```
*/
The QueryTrigger
controller handles query trigger query pipeline rules, which let you define a search query to execute when a certain condition is met.
Methods
undo
Undoes a query trigger’s correction.
subscribe
Adds a callback that’s invoked on state change.
Parameters
-
listener:
() => void
A callback that’s invoked on state change.
Returns Unsubscribe
: A function to remove the listener.
Attributes
state
The state of the QueryTrigger
controller.
Properties
-
newQuery:
string
The new query returned by a query trigger rule in the query pipeline.
-
originalQuery:
string
The original query that was submitted by the user.
-
wasQueryModified:
boolean
Whether the original query was modified by a trigger rule in the query pipeline.
Initialize
buildQueryTrigger
Creates a QueryTrigger
controller instance.
Parameters
-
engine:
SearchEngine
The headless engine.
Returns QueryTrigger
Related Types
Unsubscribe
Call signatures
-
(): void;