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