import type { ForwardedRef } from 'react' import { useCallback } from 'react' const forwardRef = (ref: ForwardedRef, value: T) => { if (typeof ref === 'function') { ref(value) } else if (ref) { ref.current = value } } /** * This hook allows to forward ref to multiple holders. * * @example * * const ref1 = useRef(null) * const ref2 = useRef(null) * * const ref = useMultipleForwardRefs([ref1, ref2]) * *
* * console.log(ref1.current) //
* console.log(ref2.current) //
*/ const useMultipleForwardRefs = (refs: ForwardedRef[]) => useCallback( (refValue: T) => { for (const ref of refs) { forwardRef(ref, refValue) } }, [...refs] ) export default useMultipleForwardRefs