import { forwardRef , ForwardRefExoticComponent , MutableRefObject , PropsWithChildren , PropsWithoutRef , RefAttributes , RefForwardingComponent , useEffect , useRef , } from 'react' ; export default function useEnsuredForwardedRef < T > ( forwardedRef ) { const ensuredRef = useRef ( forwardedRef && forwardedRef . current ) ; useEffect ( ( ) => { if ( ! forwardedRef ) { return ; } forwardedRef . current = ensuredRef . current ; } , [ forwardedRef ] ) ; return ensuredRef ; } export function ensuredForwardRef < T , P = { } > ( Component ) { return forwardRef ( ( props , ref ) => { const ensuredRef = useEnsuredForwardedRef ( ref as MutableRefObject < T > ) ; return Component ( props , ensuredRef ) ; } ) ; }