【问题标题】:Inserting the iframe into react component将 iframe 插入反应组件
【发布时间】:2016-02-28 01:54:27
【问题描述】:

我有一个小问题。从服务请求数据后,我得到了一个 iframe 代码作为响应。

<iframe src="https://www.example.com/show?data..." width="540" height="450"></iframe>

我想将它作为道具传递给我的模态组件并显示它,但是当我在渲染函数中简单地 {this.props.iframe} 它时,它显然将其显示为字符串。

在 react 或使用 JSX 时将其显示为 html 的最佳方式是什么?

【问题讨论】:

标签: iframe reactjs


【解决方案1】:

你可以像这样使用dangerouslySetInnerHTML属性

const Component = React.createClass({
  iframe: function () {
    return {
      __html: this.props.iframe
    }
  },

  render: function() {
    return (
      <div>
        <div dangerouslySetInnerHTML={ this.iframe() } />
      </div>
    );
  }
});

const iframe = '<iframe src="https://www.example.com/show?data..." width="540" height="450"></iframe>'; 

ReactDOM.render(
  <Component iframe={iframe} />,
  document.getElementById('container')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="container"></div>

另外,您可以从包含&lt;iframe&gt; 标记的字符串根据问题,您将iframe 作为字符串从服务器获取)复制所有属性和将它传递给新的&lt;iframe&gt; 标签,就像那样

/**
 * getAttrs
 * returns all attributes from TAG string
 * @return Object
 */
const getAttrs = (iframeTag) => {
  var doc = document.createElement('div');
  doc.innerHTML = iframeTag;

  const iframe = doc.getElementsByTagName('iframe')[0];
  return [].slice
    .call(iframe.attributes)
    .reduce((attrs, element) => {
      attrs[element.name] = element.value;
      return attrs;
    }, {});
}

const Component = React.createClass({
  render: function() {
    return (
      <div>
        <iframe {...getAttrs(this.props.iframe) } />
      </div>
    );
  }
});

const iframe = '<iframe src="https://www.example.com/show?data..." width="540" height="450"></iframe>'; 

ReactDOM.render(
  <Component iframe={iframe} />,
  document.getElementById('container')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="container"><div>

【讨论】:

  • 这样使用有什么缺点? Dangerously 听起来……嗯……很危险
  • 仅当您对“iframe”有很好的控制时才使用危险的SetInnerHTML,否则它将使您的用户面临跨站点脚本(XSS)攻击。
【解决方案2】:

使用 ES6,您现在可以这样做

要加载的示例 Codepen URl

const iframe = '<iframe height="265" style="width: 100%;" scrolling="no" title="fx." src="//codepen.io/ycw/embed/JqwbQw/?height=265&theme-id=0&default-tab=js,result" frameborder="no" allowtransparency="true" allowfullscreen="true">See the Pen <a href="https://codepen.io/ycw/pen/JqwbQw/">fx.</a> by ycw(<a href="https://codepen.io/ycw">@ycw</a>) on <a href="https://codepen.io">CodePen</a>.</iframe>'; 

加载 Iframe 的函数组件

function Iframe(props) {
  return (<div dangerouslySetInnerHTML={ {__html:  props.iframe?props.iframe:""}} />);
}

用法:

import React from "react";
import ReactDOM from "react-dom";
function App() {
  return (
    <div className="App">
      <h1>Iframe Demo</h1>
      <Iframe iframe={iframe} />,
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

在 CodeSandbox 上编辑:

https://codesandbox.io/s/react-iframe-demo-g3vst

【讨论】:

    【解决方案3】:

    如果您不想使用危险的SetInnerHTML,那么您可以使用下面提到的解决方案

    var Iframe = React.createClass({     
      render: function() {
        return(         
          <div>          
            <iframe src={this.props.src} height={this.props.height} width={this.props.width}/>         
          </div>
        )
      }
    });
    
    ReactDOM.render(
      <Iframe src="http://plnkr.co/" height="500" width="500"/>,
      document.getElementById('example')
    );
    

    这里有现场演示Demo

    【讨论】:

    • 并不是我不想使用它,我不是 100% 确定它是如何出错的。你的解决方案很干净我只需要我解析字符串来提取值。
    • 根据我在 React 中的理解,使用 dangerouslySetInnerHTML 不是一个好习惯,如果您认为我的解决方案是一个答案,请接受它
    • 感谢这段代码.. 这比使用 dangersoulySetHtml 更好
    • @Dhaval Patel 这个解决方案很好,但是,根据问题,这并不能解决问题 - 从服务请求数据后,我得到了一个 iframe 代码作为响应 - 似乎 iframe 是一个字符串,在这种情况下如何渲染它?
    • @AlexanderT.: 你能发布一些你想要渲染的代码吗
    猜你喜欢
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多