score:4

Accepted answer

what you want to do is in your componentdidmount, run the script to set the height. [if you are loading external content, you might want to add event listener on the iframe to wait until the external content is loaded.]

   componentdidmount() {
       const obj = reactdom.finddomnode(this);
       obj.style.height = obj.contentwindow.document.body.scrollheight + 'px';
    }

there is another, more "reacty" way of doing this - where you would store the height in state.

   componentdidmount() {
       const obj = reactdom.finddomnode(this);
       this.setstate({iframeheight:  obj.contentwindow.document.body.scrollheight + 'px'});
    }

and then in your render:

render() {
    return (
        <div style={{maxwidth:640, width:'100%', height:this.state.iframeheight, overflow:'auto'}}>
            {this.renderhtmlframe()}
        </div>
    );
}

score:0

the best and reliable way to fit iframe is to use

iframe-resizer package.

https://www.npmjs.com/package/iframe-resizer

score:0

just use usestate and useeffect with settimeout with 100miliseconds and you are done

 const [frameheight , setframeheight] = usestate()

useeffect(() => {

 const frame = document.getelementbyid('myframe');
 console.log("height" , frame.contentwindow.document.body.scrollheight + "px")
        
 settimeout(() => {
   setframeheight(frame.contentwindow.document.body.scrollheight + "px")
  },100)


 },[])
       return (
            <iframe srcdoc={content}
            id="myframe"
            width="100%" 
            height={frameheight}
            frameborder="0"
            scrolling="no"
            ></iframe>
     )

you'r welcome !

score:1

none of the answers proposed so far worked for me. the hackish approach of doing a short settimeout from within onload kind-of seems to do the job, at least in my case.

class smartiframe extends react.component {
    render() {
        return <iframe srcdoc={this.props.srcdoc}
                       scrolling="no"
                       frameborder={0}
                       width="100%"
                       onload = {e => settimeout(() => {
                           const obj = reactdom.finddomnode(this);
                           obj.style.height = obj.contentwindow.document.body.scrollheight + 'px';
                       }, 50)}/>
    }
}

score:1

this npm package will do what you what, it offers a range of different ways to calculate the height of the content in the iframe

https://www.npmjs.com/package/iframe-resizer-react

with this use case it can be configured as follows

<iframeresizer
    heightcalculationmethod="bodyscroll"
    src="http://anotherdomain.com/iframe.html"
/>

score:7

a couple of things to note here:

  • you can use refs to get a reference to the iframe instead of having to search for it
  • use the onload() handler from the iframe to ensure that the content has loaded before you try to resize it - if you try to use react's lifecycle methods like componentdidmount() you run the risk of the content not being present yet.
  • you will likely also want a resize handler to ensure the iframe gets resized as needed - just be sure to clean it up when the component unmounts.
  • you have to be careful of how different browsers report the height. go for the largest you can find.
  • you may have issues if the iframe content is in a different domain than your code. there are solutions out there such as react-iframe-resizer-super that try to solve this problem in a cross-domain compatible way.

class wrappedframe extends react.component {
  state = { contentheight: 100 };

  handleresize = () => {
    const { body, documentelement } = this.container.contentwindow.document;
    const contentheight = math.max(
      body.clientheight,
      body.offsetheight,
      body.scrollheight,
      documentelement.clientheight,
      documentelement.offsetheight,
      documentelement.scrollheight
    );
    if (contentheight !== this.state.contentheight) this.setstate({ contentheight });
  };
  
  onload = () => {
    this.container.contentwindow.addeventlistener('resize', this.handleresize);
    this.handleresize();
  }
  
  componentwillunmount() {
    this.container.contentwindow.removeeventlistener('resize', this.handleresize);
  }
  
  render() {
    const { contentheight } = this.state;
    return (
      <iframe
        frameborder="0"
        onload={this.onload}
        ref={(container) => { this.container = container; }}
        scrolling="no"
        src="your.source"
        style={{ width: '100%', height: `${contentheight}px` }}
        title="some content"
      />
    );
  }
}

in this example we're storing the determined content height in the component's state and using that state to set the height of the rendered iframe. also, by putting the onload() handler definition in the component, you save a tiny bit of performance in render() by not creating a new handler function on every re-render.

score:12

here is the answer, but first two important things.

  • iframe has to be the root component in the render() method
  • the height has to be captured from the onload event (once the iframe if fully loaded)

here is the full code:

import react, { component, proptypes } from 'react'
import reactdom from 'react-dom'

export default class fullheightiframe extends component {

    constructor() {
        super();
        this.state = {
            iframeheight: '0px'
        }
    }

    render() {
        return (
            <iframe 
                style={{maxwidth:640, width:'100%', height:this.state.iframeheight, overflow:'visible'}}
                onload={() => {
                    const obj = reactdom.finddomnode(this);
                    this.setstate({
                        "iframeheight":  obj.contentwindow.document.body.scrollheight + 'px'
                    });
                }} 
                ref="iframe" 
                src="http://www.example.com" 
                width="100%" 
                height={this.state.iframeheight} 
                scrolling="no" 
                frameborder="0"
            />
        );
    }
}

Related Query

More Query from same tag