All files / Rindu/hooks useEventListener.ts

62.5% Statements 5/8
66.66% Branches 2/3
66.66% Functions 2/3
62.5% Lines 5/8

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 3019x                   52x             52x 48x 48x                    
import { useEffect } from "react";
 
interface IUseEventListener {
  target: Element | HTMLElement | Document | Window | null;
  type: string;
  listener: EventListenerOrEventListenerObject;
  options?: boolean | AddEventListenerOptions;
  ignore?: boolean;
}
 
export function useEventListener({
  target,
  type,
  listener,
  options,
  ignore,
}: IUseEventListener): void {
  useEffect(() => {
    if (!target || ignore) {
      return;
    }
 
    target.addEventListener(type, listener, options);
 
    return () => {
      target.removeEventListener(type, listener, options);
    };
  }, [target, type, listener, options, ignore]);
}