之前運行每當我嘗試從componentWillMount中獲取我的結果時,應用程序首先渲染render(),然後它碰到componentWillMount,獲取我的結果,設置狀態,然後再次點擊渲染。爲什麼我的渲染函數在我的componentWillMount
componentWillMount=() => {
let team = gh.getRepo('Microsoft', 'vscode');
team.getContributors(function(err, members){
}).then(response => {
this.setState({
data: response.data
});
});
}
render() {
var ghList = this.state.data;
const names = ghList.map(name => { //when it runs the first time this map in invalid since ghList is null from my this.setState= {data:null}
})
return (
<div className="flip-container" onClick={this.onTileClick}>
<div className="flipper">
<div className="front">
<img className="circle" src={this.state.avatar_url} alt={this.state.login}/>
</div>
<div className="back">
</div>
</div>
</div>
)
你有一個jsfiddle或我們試過的東西嗎? –
建議您使用componentDidMount進行AJAX調用。看到http://stackoverflow.com/questions/27139366/why-do-the-react-docs-recommend-doing-ajax-in-componentdidmount-not-componentwi – ryandrewjohnson
作出改變,謝謝我將它添加到我的開發筆記。 – pcproff