javascript - Handle iframe in React with refs - Stack Overflow

I am trying to set the content of an iframe in a React ponent. I have a ponent in which contains a hand

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();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745212979a4616952.html

相关推荐

  • javascript - Handle iframe in React with refs - Stack Overflow

    I am trying to set the content of an iframe in a React ponent. I have a ponent in which contains a hand

    8小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信