A couple of things to note here:
componentDidMount()
you run the risk of the content not being present yet.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.
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"
/>
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)}/>
}
}
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>
);
}
Here is the answer, but first two important things.
render()
methodonLoad
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"
/>
);
}
}