您要做的是在您的componentDidMount中,运行脚本来设置高度。[如果要加载外部内容,则可能要在IFrame上添加事件侦听器,以等待加载外部内容。
componentDidMount() {
const obj = ReactDOM.findDOMNode(this);
obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}
还有另一种更“反应”的方式-将高度存储在状态中。
componentDidMount() {
const obj = ReactDOM.findDOMNode(this);
this.setState({iFrameHeight: obj.contentWindow.document.body.scrollHeight + 'px'});
}
然后在您的渲染器中:
render() {
return (
<div style={{maxWidth:640, width:'100%', height:this.state.iFrameHeight, overflow:'auto'}}>
{this.renderHTMLFrame()}
</div>
);
}