admin管理员组文章数量:1415145
I am trying to set the content of an iframe in a React ponent. I have a ponent in which contains a handleStatementPrint
function which has to be called when the iframe finishes loading. That function must print loaded iframe content - pdf file accessed with URL this.props.pdfs.url
. Already iframe content is loaded and I can see pdf file in iframe, but I need to pass iframe content with refs but don't know how to do that correctly. I know that I need to use ponentDidMount
, but don't know that to write in here.
Component which must have iframe content:
import React, { Component } from 'react'
import IframeComponent from './ponents/Iframe';
class MainComponent extends Component {
handleStatementPrint = () => {
const iframePdf = this.iframePdf.contentWindow;
if (this.iframePdf !== undefined) {
const iframePdf = this.iframePdf.contentWindow;
iframePdf.print();
}
}
render () {
return (
<div className="container">
{
this.props.pdfs &&
<iframe
ref={(frame) => { this.iframePdf = frame }}
src={this.props.pdfs.url}
title="iFramePdf"
type="application/pdf"
>
</iframe>
}
</div>
);
}
};
export default Statement;
Iframe ponent:
import React, { Component } from 'react'
class IframeComponent extends Component {
ponentDidMount() {
// Load iframe content
}
render () {
return (
<div>
<Iframe />
</div>
);
}
};
export default Iframe;
I'm tried this examples:
Basic react iframe with onLoad handler
Handling of iframes in React
Iframe content is ing from fetch API, but I can access iframe and can see that content is perfectly loaded using ref. Problem: need to load that content in ponentDidMount
method before calling handleStatementPrint
function from another ponent which can print loaded iframe content.
Questions:
So how to pass correctly iframe content with refs to load content in ponentDidMountmethod?
How to pass loaded content from
ponentDidMount
method in MainComponent functions, to do actions with loaded content?
I am trying to set the content of an iframe in a React ponent. I have a ponent in which contains a handleStatementPrint
function which has to be called when the iframe finishes loading. That function must print loaded iframe content - pdf file accessed with URL this.props.pdfs.url
. Already iframe content is loaded and I can see pdf file in iframe, but I need to pass iframe content with refs but don't know how to do that correctly. I know that I need to use ponentDidMount
, but don't know that to write in here.
Component which must have iframe content:
import React, { Component } from 'react'
import IframeComponent from './ponents/Iframe';
class MainComponent extends Component {
handleStatementPrint = () => {
const iframePdf = this.iframePdf.contentWindow;
if (this.iframePdf !== undefined) {
const iframePdf = this.iframePdf.contentWindow;
iframePdf.print();
}
}
render () {
return (
<div className="container">
{
this.props.pdfs &&
<iframe
ref={(frame) => { this.iframePdf = frame }}
src={this.props.pdfs.url}
title="iFramePdf"
type="application/pdf"
>
</iframe>
}
</div>
);
}
};
export default Statement;
Iframe ponent:
import React, { Component } from 'react'
class IframeComponent extends Component {
ponentDidMount() {
// Load iframe content
}
render () {
return (
<div>
<Iframe />
</div>
);
}
};
export default Iframe;
I'm tried this examples:
Basic react iframe with onLoad handler
Handling of iframes in React
Iframe content is ing from fetch API, but I can access iframe and can see that content is perfectly loaded using ref. Problem: need to load that content in ponentDidMount
method before calling handleStatementPrint
function from another ponent which can print loaded iframe content.
Questions:
So how to pass correctly iframe content with refs to load content in ponentDidMountmethod?
How to pass loaded content from
ponentDidMount
method in MainComponent functions, to do actions with loaded content?
1 Answer
Reset to default 0This is how refs works:
<MyComponent ref={(c) => { this.myComponent = c; }} />
After ponent is MOUNTED you gain access to the ponent itself and its method:
this.myComponent.myMethod();
本文标签: javascriptHandle iframe in React with refsStack Overflow
版权声明:本文标题:javascript - Handle iframe in React with refs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745212978a2647997.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论