@razorpay/blade
Version:
The Design System that powers Razorpay
38 lines (32 loc) • 1.75 kB
JavaScript
import React__default, { useRef, useCallback } from 'react';
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect.js';
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-function-return-type */
// Prevent webpack from importing this:
// https://github.com/webpack/webpack/issues/14814#issuecomment-1536757985
// https://github.com/radix-ui/primitives/issues/2796
var useReactInsertionEffect = React__default[' useInsertionEffect '.trim().toString()];
var useInsertionEffectFallback = useReactInsertionEffect || useIsomorphicLayoutEffect;
/**
* This hook is user-land implementation of the experimental `useEffectEvent` hook.
* React docs: https://react.dev/learn/separating-events-from-effects#declaring-an-effect-event
*
* This hook is useful when you want to use a callback function inside a useEffect while keeping it "fresh" but you don't want to add it to the dependency array.
*/
function useCallbackRef(callback) {
var deps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var callbackRef = useRef(callback);
useInsertionEffectFallback(function () {
callbackRef.current = callback;
});
// eslint-disable-next-line react-hooks/exhaustive-deps
return useCallback(function () {
var _callbackRef$current;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return (_callbackRef$current = callbackRef.current) === null || _callbackRef$current === void 0 ? void 0 : _callbackRef$current.call.apply(_callbackRef$current, [callbackRef].concat(args));
}, deps);
}
export { useCallbackRef };
//# sourceMappingURL=useCallbackRef.js.map