mirror of
https://github.com/standardnotes/app
synced 2026-10-20 21:03:15 -04:00
20 lines
399 B
TypeScript
20 lines
399 B
TypeScript
import { ForwardedRef, useEffect, useRef } from 'react'
|
|
|
|
export const useForwardedRef = <T,>(ref: ForwardedRef<T>, initialValue = null) => {
|
|
const targetRef = useRef<T>(initialValue)
|
|
|
|
useEffect(() => {
|
|
if (!ref) {
|
|
return
|
|
}
|
|
|
|
if (typeof ref === 'function') {
|
|
ref(targetRef.current)
|
|
} else {
|
|
ref.current = targetRef.current
|
|
}
|
|
}, [ref])
|
|
|
|
return targetRef
|
|
}
|