2016-03-16 99 views
0

我有以下的(我没有写):阵营格式问题

render: function() { 
     if(this.state.loading){ 
      return <div><span><i className="fa fa-spinner fa-spin"></i> Loading...</span></div> 
     }else if(typeof this.state.listing.id == 'undefined' || !this.state.component){ 
      return false; 
     } 
     return (
      React.createElement(eval(this.state.component), { 
       listing: this.state.listing, 
       onClose: this.handleClose, 
       options: this.state.options 
      }) 
     ); 
    } 

这是一个React.creatClass内。我是陌生的反应,但只需要一个包装DIV解决此部分:

   React.createElement(eval(this.state.component), { 
        listing: this.state.listing, 
        onClose: this.handleClose, 
        options: this.state.options 
       }) 

如何添加围绕一块简单的div,所以我可以正常样式该组件?

+2

'eval(this.state.component)'哇,这看起来像一个反模式。 – Mathletics

回答

1

只是将它包装在另一个JSX标签中。如果你能摆脱那个流浪的React.createElement调用并且只使用JSX,那将是最好的。

render: function() { 
    if(this.state.loading){ 
    return (
     <div> 
     <span> 
      <i className="fa fa-spinner fa-spin"></i> 
      Loading... 
     </span> 
     </div> 
    ); 
    } else if(typeof this.state.listing.id == 'undefined' || !this.state.component) { 
    return false; 
    } 
    return (
    <div> 
     {React.createElement(eval(this.state.component), { 
     listing: this.state.listing, 
     onClose: this.handleClose, 
     options: this.state.options}) 
    )} 
    </div> 
); 
} 

真正的问题在这里,究竟是什么eval在那里做?