RedirectionTrigger
RedirectionTrigger
This is for:
DeveloperIn this article
Example Implementation
redirection-trigger.fn.tsx
import {RedirectionTrigger as HeadlessRedirectionTrigger} from '@coveo/headless';
import {useEffect, useState, FunctionComponent} from 'react';
interface HeadlessRedirectionTriggerProps {
controller: HeadlessRedirectionTrigger;
}
export const RedirectionTrigger: FunctionComponent<
HeadlessRedirectionTriggerProps
> = (props) => {
const {controller} = props;
const [state, setState] = useState(controller.state);
useEffect(() => controller.subscribe(() => redirect()), []);
useEffect(() => redirect(), [state.redirectTo]);
const redirect = () => {
setState(props.controller.state);
if (state.redirectTo) {
window.location.replace(controller.state.redirectTo);
}
};
return null;
};
The RedirectionTrigger
controller handles redirection triggers. A Redirection trigger query pipeline rule lets you define a URL to redirect the user’s browser to when a certain condition is met.
Methods
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 RedirectionTrigger
controller.
Properties
-
redirectTo:
string
The url used for the redirection.
Initialize
buildRedirectionTrigger
Creates a RedirectionTrigger
controller instance.
Parameters
-
engine:
SearchEngine
The headless engine.
Returns RedirectionTrigger
Related Types
Unsubscribe
Call signatures
-
(): void;