简体   繁体   English

React js以Bootstrap模式显示动态HTML

[英]React js Display dynamic html in bootstrap modal

I am trying to display Unordered List inside the Bootstrap Modal in React JS , the list is dynamic. 我正在尝试在React JS的Bootstrap Modal中显示Unordered List,该列表是动态的。

I tried this way to achieve but it doesn't work the html elements appears as a string 我尝试过这种方式来实现,但它不起作用html元素显示为字符串

var ModalHeader = React.createClass({
 render: function () {
return (
  <div className="modal-header">
    {this.props.title}
    <button type="button" className="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
)
}});

var ModalBody = React.createClass({
render: function () {
return (
  <div className="modal-body">
    {this.props.content}
  </div>
)
}});

var ModalFooter = React.createClass({
render: function () {
return (
  <div className="modal-footer">
    <button type="button" className="btn btn-primary">Submit</button>
  </div>
)
}});

var Modal = React.createClass({
render: function () {
return (<div className="modal fade">
    <div className="modal-dialog">
      <div className="modal-content">
        <ModalHeader title="Modal Title"/>
        <ModalBody content = "<ul><li>Make</li><li>Hub</li><li>Market</li><li>Assembly type</li><li>Order Number</li></ul>" />
        <ModalFooter />
      </div>
    </div>
  </div>)
}});

var Button = React.createClass({
  showModal: function() {
$(this.refs.modal.getDOMNode()).modal();
},
render : function(){
return (
  <div>
    <button className="btn btn-default" onClick={this.showModal}>
        Show Modal
   </button>
    <Modal ref="modal" />
  </div>
);
 }});

React.render(<Button />, document.getElementById('container'));

Is there any other approach to display dynamic html inside Bootstrap Model/Popup in React JS . 还有什么其他方法可以在React JS的Bootstrap Model / Popup内部显示动态html。 Thanks in advance. 提前致谢。

As stacks26 pointed out in a comment , dangerouslySetInnerHtml is available if you have a string of raw HTML. 作为stacks26中指出的评论dangerouslySetInnerHtml若您有原始的HTML的字符串。 Your ModalBody component's render method would look like this: 您的ModalBody组件的render方法将如下所示:

return (
  <div className="modal-body"
       dangerouslySetInnerHTML={{__html: this.props.content}} />
);

This works, but comes with the inherent danger implied by this prop's name – you have to be very careful where all of that HTML is coming from, lest you set yourself up for a cross-site scripting vulnerability. 此方法有效,但是带有该道具名称所隐含的固有危险–您必须非常小心所有 HTML的来源,以免您为跨站点脚本漏洞设置自己。

However, in your example, you have your modal contents verbatim in your code. 但是,在您的示例中,您的模态内容逐字记录在代码中。 I don't know if that's just for illustration purposes, and in your real use case that rendered HTML is from elsewhere. 我不知道这是否仅用于说明目的,在您的实际用例中,呈现的HTML来自其他地方。 But if you're mainly looking for a way to "give content" to a component, but it's not necessarily given as a raw HTML string, there are better ways. 但是, 如果您主要是在寻找一种向组件“提供内容”的方法,但不一定将其作为原始HTML字符串给出,则有更好的方法。

First, a prop's value doesn't have to be a string, it can be any JavaScript expression – and that includes JSX. 首先,道具的值不必是字符串,它可以是任何JavaScript表达式-包括JSX。 So this works fine: 所以这很好用:

<ModalBody content={<ul><li>Make</li><li>...</li></ul>} />

But even better, cases like this are exactly what the magic children prop is made for. 但更好的是,像这样的情况正是魔术children道具的用途。 This prop receives whatever you have between your opening and closing JSX tags. 该道具会接收您在打开和关闭JSX标签之间所拥有的一切。

So if you use the children prop instead of content in your ModalBody , like this: 所以,如果你使用的children托,而不是contentModalBody ,就像这样:

return (
  <div className="modal-body">
    {this.props.children}
  </div>
);

and render your Modal like this: 并像这样渲染您的Modal

return (<div className="modal fade">
        <div className="modal-dialog">
          <div className="modal-content">
            <ModalHeader title="Modal Title"/>
            <ModalBody>
              <ul>
                <li>Make</li>
                <li>Hub</li>
                <li>Market</li>
                <li>Assembly type</li>
                <li>Order Number</li>
              </ul>
            </ModalBody>
            <ModalFooter />
          </div>
        </div>
      </div>)

then you're expressing the fact that the <ul> is the content of the <ModalBody> in a much more natural way, by enclosing it between tags. 那么您<ModalBody>更自然的方式表达<ul><ModalBody>的内容的事实,方法是将其包含在标签之间。

You can also do the same thing with the ModalHeader . 您也可以使用ModalHeader进行相同的ModalHeader

Here's a snippet that's mostly copied from your question, but uses props.children for header and body of the modal: 以下是大部分是从您的问题中复制的代码段,但使用props.children作为模态的标题和正文:

 var ModalHeader = React.createClass({ render: function () { return ( <div className="modal-header"> {this.props.children} <button type="button" className="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> ) }}); var ModalBody = React.createClass({ render: function () { return ( <div className="modal-body"> {this.props.children} </div> ) }}); var ModalFooter = React.createClass({ render: function () { return ( <div className="modal-footer"> <button type="button" className="btn btn-primary">Submit</button> </div> ) }}); var Modal = React.createClass({ render: function () { return (<div className="modal fade"> <div className="modal-dialog"> <div className="modal-content"> <ModalHeader> Modal Title </ModalHeader> <ModalBody> <ul><li>Make</li><li>Hub</li><li>Market</li><li>Assembly type</li><li>Order Number</li></ul> </ModalBody> <ModalFooter /> </div> </div> </div>) }}); var Button = React.createClass({ showModal: function() { $(this.refs.modal.getDOMNode()).modal(); }, render : function(){ return ( <div> <button className="btn btn-default" onClick={this.showModal}> Show Modal </button> <Modal ref="modal" /> </div> ); }}); ReactDOM.render(<Button />, document.getElementById('container')); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script> <div id="container"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM