Setting iframe height to scrollHeight in ReactJS

后端 未结 5 501
隐瞒了意图╮
隐瞒了意图╮ 2021-01-02 07:24
  • The typical solution to the problem doesn\'t work in in React due to its dynamically generated component structure and event model, as opposed to traditional static HT
5条回答
  •  长情又很酷
    2021-01-02 08:02

    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 (