THIS IS ARCHIVED DOCUMENTATION
RedirectionTrigger (Deprecated)
RedirectionTrigger (Deprecated)
In this article
In this article
The RedirectionTrigger
controller handles redirection actions.
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
Example Implementation
redirection-trigger.fn.tsx
import {useEffect, useState, FunctionComponent} from 'react';
import {RedirectionTrigger as HeadlessRedirectionTrigger} from '@coveo/headless';
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.href = controller.state.redirectTo!;
}
};
return null;
};