import { useRef } from 'react';

function useEventCallback<T>(handler?: (value: T) => void): (value: T) => void {
  const callbackRef = useRef(handler);
  const fn = useRef((value: T) => {
    if (callbackRef.current) {
      callbackRef.current(value);
    }
  });
  callbackRef.current = handler;

  return fn.current;
}

export default useEventCallback;
