reactjs - iframe 的 useRef 使用什么类型

标签 reactjs typescript iframe

我想避免使用 any 类型我找不到我应该为它使用什么类型定义:

const iframe = useRef<any>();

<iframe
    ref={iframe}
    sandbox='allow-scripts'
    srcDoc={rootHtml}
/>

以便以后的 Typescript 可以识别 iframe.current 的属性如 iframe.current.contentWindowiframe.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/

相关文章:

cordova - 在 Cordova 上保护 postMessage()

javascript - 从 document.ready ie8 设置 iFrame 高度

css - 悬停时如何更改特定元素?

javascript - 将 tsconfig.json 属性添加到 Gulp 任务

javascript - 如何通过Angular2中的链接发送基于视频上传的放置请求

typescript - Svelte 的类型 'object' 上不存在属性(使用 TypeScript)

css - 为什么缩放的 iframe 水平滚动

node.js - [ MongoNetworkError ],当我在 Heroku 上部署时,应用程序无法连接到 mongoDB

javascript - webpack 中的 css 配置不检测类样式但检测元素样式

reactjs - React 数据网格过滤器默认启用(并打开)