【问题标题】:async load iframe in react在反应中异步加载 iframe
【发布时间】:2019-02-01 07:56:45
【问题描述】:

我在网页上使用 iframe,它会阻止页面的其余部分呈现,直到 iframe 完全加载。如何启用异步 iframe 加载(或延迟 iframe 的加载),以免网页元素被阻止? *iframe 不提供异步加载。

【问题讨论】:

    标签: reactjs iframe redux


    【解决方案1】:

    您可以像这样在 componentDidMount 之后初始化 iframe:

    class MyComponent extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                showIframe: false
            };
        }
    
        componentDidMount() {
            this.setState({showIframe: true});
        }
    
        render() {
            const { showIframe } = this.state;
            return (
                <div>
                    { showIframe && 
                        <iframe src={'https://www.example.com'} />
                    }
                </div>
            );
        }
    }
    

    这将在您的组件安装后呈现 iframe。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-27
      相关资源
      最近更新 更多