[英]React.ref prop in interface
我嘗試將字符串作為參考,但是當我嘗試使用 ref?.current 時收到警告
Property 'current' does not exist on type 'string'
接口中的 ref prop 應該使用什么。 我不知道 ref 類型是什么。 它可以是 HTMLInputElement 也可以是 div 基本上可以是任何元素。
interface IProps {
name: string,
time: number
ref?: string
}
React 中的 Ref 通常由以下接口描述: React.Ref<HTMLInputElement>
。
嘗試:
interface IProps {
name: string,
time: number
ref?: React.Ref<HTMLInputElement>
}
TypeScript 不會讓您傳遞泛型(如:事先未知) React.Ref<HTMLElement>
或React.Ref<Element>
。
您需要使用any
來欺騙類型檢查器以接受您的 ref。
interface Props {
name: string;
time: number;
ref?: React.Ref<any>;
}
請記住,此解決方案並非完全類型安全。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.