2016-11-16 79 views
0

我試圖運行一個簡單的React組件,但不知道爲什麼這不起作用。反應:在變量內調用方法

由於某種原因,addRows未運行。有人能解釋爲什麼嗎?

var CreateRows = React.createClass({ 
    addRows : function(){ 
     return (
       <tr> 
        <td>Morgan</td> 
        <td>[email protected]</td> 
       </tr> 

      ) 
    }, 

    render: function(){ 
     return (
      <tbody> 
       {this.addRows} 
      </tbody> 
     ) 
    } 
}); 
+0

調用方法 - {this.addRows()} –

+0

謝謝。我一直在關注這個教程 - https://tylermcginnis.com/react-js-tutorial-pt-1-a-comprehensive-guide-to-building-apps-with-react-js-8ce321b125ba#.5w3ath7hc他們不喜歡不要用()來調用它中的函數,但它可以工作。你能解釋爲什麼嗎? –

回答

2

你必須調用函數{ this.addRows() }。用你的代碼你只能引用這個函數,但不能調用它。這樣的陳述可以用於定義一個功能,例如像onClick={ this.addRows }

+0

啊,這解釋了爲什麼問題的評論!謝謝。 –

+0

太好了:)請在這種情況下接受我的回答是正確的;) – mrunde