lndsld

Home > @lndsld/react-hooks > useForwardedRef

useForwardedRef() function

Make proxy for ForwardedRef

Signature:

declare function useForwardedRef<T>(forwardedRef: ForwardedRef<T> | undefined): RefObject<T>;

Parameters

Parameter Type Description
forwardedRef ForwardedRef<T> | undefined  

Returns:

RefObject<T>

Example

import { forwardRef, useEffect } from 'react';

const Component = forwardRef((props, forwardedRef) => {
 const ref = useForwardedRef(forwardedRef);

 useEffect(() => {
   ref.current?.focus()
 }, [ref])

 return <input type="text" ref={ref} />
})