2016-03-16 42 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在那裏做?