import { useRef, useMemo, useEffect } from 'react' /** * A custom hook that converts a callback to a ref to avoid triggering re-renders when passed as a * prop or avoid re-executing effects when passed as a dependency */ function useStableCallback any>( callback: T | undefined ): T { const callbackRef = useRef(callback) useEffect(() => { callbackRef.current = callback }) // https://github.com/facebook/react/issues/19240 return useMemo(() => ((...args) => callbackRef.current?.(...args)) as T, []) } export { useStableCallback }