"use client" import * as React from "react" // basically Exclude["ref"], string> type UserRef = | ((instance: T | null) => void) | React.RefObject | null | undefined const updateRef = (ref: NonNullable>, value: T | null) => { if (typeof ref === "function") { ref(value) } else if (ref && typeof ref === "object" && "current" in ref) { // Safe assignment without MutableRefObject ;(ref as { current: T | null }).current = value } } export const useComposedRef = ( libRef: React.RefObject, userRef: UserRef ) => { const prevUserRef = React.useRef>(null) return React.useCallback( (instance: T | null) => { if (libRef && "current" in libRef) { ;(libRef as { current: T | null }).current = instance } if (prevUserRef.current) { updateRef(prevUserRef.current, null) } prevUserRef.current = userRef if (userRef) { updateRef(userRef, instance) } }, [libRef, userRef] ) } export default useComposedRef