以下に、InitialState を設定し、componentWillMount と fetchData を使用して API 呼び出しを行い、データ this.state を割り当てる関数があります。ただし、 this.setState() が完了すると、レンダー関数は新しい this.state データでトリガーされません。私の関数は以下のとおりです。
var Home = React.createClass({
getInitialState: function() {
return {
city: '',
weather: '',
temperature: 0,
humidity: 0,
wind: 0,
}
},
fetchData: function() {
apiHelpers.getCityInfo()
.then(function (response){
this.setState({ data: response
})
}.bind(this))
},
componentWillMount: function(){
this.fetchData();
},
render: function () {
return (
<div className="container">
<Cities data={this.state.data} />
</div>
)
}
});