2016-10-05 55 views
0

我有下面的代碼,它什麼都不渲染,我不知道我的錯誤在哪裏,我沒有看到控制檯中的任何錯誤。反應圖es6箭頭功能不起作用

var App = React.createClass({ 
    getInitialState(){ 
    return { 
    items:[1,2,3] 
    } 
    }, 
    renderItem(){ 
    this.state.items.map((item,i)=> <li key={i}>{item}</li>) 
    }, 
    render(){ 
     return(
     <ul> 
     {this.renderItem} 
     </ul> 
    ) 
    } 
}) 

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

http://jsfiddle.net/3Ley7uac/

需要提醒。

回答

1

首先你需要打電話給你的方法與()的:

<ul> 
    {this.renderItems()} 
    </ul> 

其次,你需要在方法內部返回:

renderItems(){ 
    return this.state.items.map((item,i)=> <li key={i}>{item}</li>) 
}, 

這些只是香草Javascript類的方法。這裏沒有什麼特別的React。您需要像使用任何Javascript代碼一樣調用方法並返回值。

+0

當你使用返回並再次調用它?但它的工作,謝謝!有沒有renderItem的自執行函數? –

+0

如果這是正確的答案,請單擊左側答案旁邊的空白複選標記以將其標記爲正確。 –

+0

我又被卡住了Andy http://stackoverflow.com/questions/39865363/pass-index-from-function-to-function-in-react –