简体   繁体   English

ReactJS 中 iFrame 的 document.getElementById

[英]document.getElementById for iFrame in ReactJS

In my site, I need to send information (via postMessage) to an iFrame.在我的站点中,我需要将信息(通过 postMessage)发送到 iFrame。 I know in regular Javascript, I would accomplish this by using document.getElementById or $("#iframe") in JQuery to select the iframe. I know in regular Javascript, I would accomplish this by using document.getElementById or $("#iframe") in JQuery to select the iframe. However, I am unsure of how to do this in ReactJS.但是,我不确定如何在 ReactJS 中执行此操作。 Is there a specific way of doing this in ReactJS/NextJS that I just don't know about?在 ReactJS/NextJS 中是否有一种我不知道的特定方法? I need access to the iframe (the child component) from its container (parent component).我需要从其容器(父组件)访问 iframe(子组件)。

If the iframe is rendered by React, and only the component that renders it (or its descendants) needs to access it, then typically you use refs .如果iframe由 React 渲染,并且只有渲染它的组件(或其后代)需要访问它,那么通常您使用refs

If the iframe is always on the page, or rendered in some way outside of React, it's perfectly fine to get it via document.getElementById , document.querySelector , or other DOM methods.如果iframe始终在页面上,或者以某种方式在 React 之外呈现,则可以通过document.getElementByIddocument.querySelector或其他 DOM 方法获取它。


Here's an example of using a ref in a functional component via useRef , but you can do the same thing (in a different way) in a class component via createRef .这是通过useRef在功能组件中使用 ref 的示例,但是您可以通过createRef在 class 组件中执行相同的操作(以不同的方式)。 I'll use a div instead of an iframe , but it's the same for iframe s.我将使用div代替iframe ,但iframe相同。

 function Example() { const ref = React.useRef(null); const doSomething = () => { if (ref.current) { console.log(`The div's text is "${ref.current.textContent}"`); } else { console.log("The div doesn't exist"); } }; return ( <div> <div ref={ref}> This is the target div </div> <input type="button" onClick={doSomething} value="Click Me" /> </div> ); } ReactDOM.render(<Example/>, document.getElementById("root"));
 <div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.12.0/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.12.0/umd/react-dom.production.min.js"></script>

Class component example: Class 组件示例:

 class Example extends React.Component { constructor(props) { super(props); this.ref = React.createRef(); this.doSomething = this.doSomething.bind(this); } doSomething() { if (this.ref.current) { console.log(`The div's text is "${this.ref.current.textContent}"`); } else { console.log("The div doesn't exist"); } } render() { return ( <div> <div ref={this.ref}> This is the target div </div> <input type="button" onClick={this.doSomething} value="Click Me" /> </div> ); } } ReactDOM.render(<Example/>, document.getElementById("root"));
 <div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.12.0/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.12.0/umd/react-dom.production.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM