* 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
(callback: T | undefined)
| 5 | * prop or avoid re-executing effects when passed as a dependency |
| 6 | */ |
| 7 | function useCallbackRef<T extends (...args: any[]) => any>(callback: T | undefined): T { |
| 8 | const callbackRef = React.useRef(callback) |
| 9 | |
| 10 | React.useEffect(() => { |
| 11 | callbackRef.current = callback |
| 12 | }) |
| 13 | |
| 14 | // https://github.com/facebook/react/issues/19240 |
| 15 | return React.useMemo(() => ((...args) => callbackRef.current?.(...args)) as T, []) |
| 16 | } |
| 17 | |
| 18 | export { useCallbackRef } |
no outgoing calls
no test coverage detected