Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 15x 3x 3x 3x 3x 2x 2x 1x 1x 1x | import { useEffect, useRef } from 'react';
import { Evented } from '@mapbox/search-js-core';
/**
* A React hook to register an event listener on a Search JS Core Evented object.
*
* {@link Evented} is a base class that is inherited by {@link SearchSession}.
*/
export function useEvented<T, K extends keyof T>(
evented: Evented<T> | null,
eventName: K,
cb: (object: T[K]) => unknown
): void {
const cbRef = useRef(cb);
useEffect(() => {
cbRef.current = cb;
});
useEffect(() => {
if (!evented) return;
const fn = (object?: T[K]) => cbRef.current(object);
evented.addEventListener(eventName, fn);
return () => {
evented.removeEventListener(eventName, fn);
};
}, [evented, eventName, cbRef]);
}
|