UNPKG

@coreui/react-pro

Version:

UI Components Library for React.js

47 lines (44 loc) 1.42 kB
import { useMemo } from 'react'; // code borrowed from https://github.com/reach/reach-ui // problem described https://github.com/facebook/react/issues/13029 // eslint-disable-next-line @typescript-eslint/no-explicit-any function useForkedRef(...refs) { return useMemo(() => { if (refs.every((ref) => ref == null)) { return null; } // eslint-disable-next-line @typescript-eslint/no-explicit-any return (node) => { refs.forEach((ref) => { assignRef(ref, node); }); }; }, // useForkedRef accepts a variable number of refs, so the dependency list // cannot be a static array literal; the refs are compared element-wise. // eslint-disable-next-line react-hooks/use-memo, react-hooks/exhaustive-deps refs); } // eslint-disable-next-line @typescript-eslint/no-explicit-any function assignRef(ref, // eslint-disable-next-line @typescript-eslint/no-explicit-any value) { if (ref == null) return; if (isFunction(ref)) { ref(value); } else { try { ref.current = value; } catch (_a) { throw new Error(`Cannot assign value "${value}" to ref "${ref}"`); } } } function isFunction(value) { return typeof value === 'function'; } export { assignRef, isFunction, useForkedRef }; //# sourceMappingURL=useForkedRef.js.map