NotifyTrigger

This is for:

Developer

The NotifyTrigger controller handles Notify triggers.

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 NotifyTrigger controller.

Properties

  • notification: string

    The notification to display to the user after receiving a notification trigger.

Initialize

buildNotifyTrigger

Creates a NotifyTrigger controller instance.

Parameters

  • engine: SearchEngine

    The headless engine.

Returns NotifyTrigger

Unsubscribe

Call signatures

  • (): void

Example Implementation

notify-trigger.fn.tsx

import {useEffect, useState, FunctionComponent} from 'react';
import {NotifyTrigger as HeadlessNotifyTrigger} from '@coveo/headless';
 
interface HeadlessNotifyTriggerProps {
  controller: HeadlessNotifyTrigger;
}
 
export const NotifyTrigger: FunctionComponent<HeadlessNotifyTriggerProps> = (
  props
) => {
  const {controller} = props;
  const [state, setState] = useState(controller.state);
 
  useEffect(() => controller.subscribe(() => updateState()), []);
  useEffect(() => notify(), [state.notification]);
 
  const updateState = () => {
    setState(props.controller.state);
  };
 
  const notify = () => {
    if (state.notification) {
      alert('Notification: ' + state.notification);
    }
  };
 
  return null;
};
 
// usage
 
/**
 * ```tsx
 * const controller = buildNotifyTrigger(engine);
 *
 * <NotifyTriggerFn controller={controller} />;
 * ```
 */