[英]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.