RedirectionTrigger

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 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

Unsubscribe

Call signatures

  • (): void