useToggleuseToggle+useEventRefuseSafeEffectuseEventControluseDebounceuseThrottleuseClickOutsideuseTabsuseDateuseDatesuseImageuseCaptureuseIntersectionObserver
react react-hook ironman30

useEventRef

A Hook to hold a latest function instance as inner function for using in useCallback.


LINKS
function useEventRef(callback) {
  if (callback && typeof callback !== "function") {
    throw new Error(
      "[useEventRef] callback that passed into hook should be a function."
    )
  }

  const eventRef = useRef(callback)

  useSafeEffect(() => {
    eventRef.current = callback
  })

  return useCallback((...args) => eventRef.current?.(...args), [])
}