将 iframe 插入 React 组件 [英] Inserting the iframe into react component
问题描述
我有一个小问题.从服务请求数据后,我得到了一个 iframe 代码作为响应.
我想将它作为道具传递给我的模态组件并显示它,但是当我在渲染函数中简单地 {this.props.iframe}
它时,它显然将它显示为字符串.
在 react 或使用 JSX 中将其显示为 html 的最佳方式是什么?
您可以使用属性 dangerouslySetInnerHTML
,像这样
const Component = React.createClass({iframe: 函数 () {返回 {__html: this.props.iframe}},渲染:函数(){返回 (<div><div risklySetInnerHTML={ this.iframe() }/>
);}});const iframe = '<iframe src="https://www.example.com/show?data..." width="540" height="450"></iframe>';ReactDOM.render(<组件 iframe={iframe}/>,document.getElementById('容器'));
<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>
此外,您可以从包含 <iframe>
标签并将其传递给新的 标签,就像那样
/*** 获取属性* 从 TAG 字符串返回所有属性* @return 对象*/const getAttrs = (iframeTag) =>{var doc = document.createElement('div');doc.innerHTML = iframeTag;const iframe = doc.getElementsByTagName('iframe')[0];返回 [].slice.call(iframe.attributes).reduce((attrs, element) => {attrs[element.name] = element.value;返回属性;}, {});}const 组件 = React.createClass({渲染:函数(){返回 (<div><iframe {...getAttrs(this.props.iframe) }/>
);}});const iframe = '<iframe src="https://www.example.com/show?data..." width="540" height="450"></iframe>';ReactDOM.render(<组件 iframe={iframe}/>,document.getElementById('容器'));
<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="容器"><div>
I have a small problem. After requesting a data from a service I got an iframe code in response.
<iframe src="https://www.example.com/show?data..." width="540" height="450"></iframe>
I would like to pass this in as a props to my modal component and display it but when I simply {this.props.iframe}
it in the render function it is obviously displaying it as a string.
What is the best way to display it as html in react or using JSX?
You can use property dangerouslySetInnerHTML
, like this
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>
also, you can copy all attributes from the string(based on the question, you get iframe as a string from a server) which contains <iframe>
tag and pass it to new <iframe>
tag, like that
/**
* 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>
这篇关于将 iframe 插入 React 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!