我想避免使用 any
类型我找不到我应该为它使用什么类型定义:
const iframe = useRef<any>();
<iframe
ref={iframe}
sandbox='allow-scripts'
srcDoc={rootHtml}
/>
以便以后的 Typescript 可以识别 iframe.current
的属性如
iframe.current.contentWindow
和 iframe.current.srcdoc
,...
*编辑
如果我尝试使用 HTMLIFrameElement
按照建议,我在 ref={iframe}
下收到此错误说:
Type 'MutableRefObject<HTMLIFrameElement | undefined>' is not >assignable to type 'LegacyRef | undefined'. Type 'MutableRefObject<HTMLIFrameElement | undefined>' is not >assignable to type 'RefObject'. Types of property 'current' are incompatible. Type 'HTMLIFrameElement | undefined' is not assignable to type >'HTMLIFrameElement | null'. Type 'undefined' is not assignable to type 'HTMLIFrameElement | >null'.ts(2322)
最佳答案
const iframe = useRef<HTMLIFrameElement>();
以后看其他类型可以引用这个文件@types/react/global.d.ts
关于reactjs - iframe 的 useRef 使用什么类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70623283/