最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Handle iframe in React with refs - Stack Overflow

programmeradmin0浏览0评论

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:

  1. So how to pass correctly iframe content with refs to load content in ponentDidMountmethod?

  2. 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:

  1. So how to pass correctly iframe content with refs to load content in ponentDidMountmethod?

  2. How to pass loaded content from ponentDidMount method in MainComponent functions, to do actions with loaded content?

Share Improve this question edited Apr 25, 2023 at 23:49 Jason Aller 3,65228 gold badges41 silver badges39 bronze badges asked Dec 4, 2017 at 14:13 User PauliusUser Paulius 2572 gold badges4 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 0

This 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();
发布评论

评论列表(0)

  1. 暂无评论