componentWillMountメソッドでAPIへの非同期呼び出しを実行しようとしています。実際にrender
メソッドのコンポーネントにprops
を渡す必要があるので、render
メソッドをcomponentWillMountメソッドの後に実行したいと思います。ここで レンダリングメソッドの後にcomponentWillMountの非同期呼び出しが終了する
class TennisSearchResultsContainer extends React.Component {
componentWillMount() {
// TODO: Build markers for the map
// TODO: Check courtsResults object and database for tennis court
this.courtsMarkers = this.props.courtsResults.map((court) => {
return new google.maps.Marker({
position: new google.maps.LatLng(JSON.parse(court.LOC).coordinates[1], JSON.parse(court.LOC).coordinates[0]),
title: court.NAME,
animation: google.maps.Animation.DROP
});
});
}
render() {
return <TennisSearchResults criterias={this.props.criterias} courtsMarkers={this.courtsMarkers} />;
}
}
...
私は正しいですか?それを修正するために私は何をすべきですか?これを処理する方法は何ですか?
代わりに 'componentDillMount'を使う必要があります。なぜなら、' componentWillMount'はコンポーネントがDOMにマウントされる前に一度だけ実行されるからです。したがって、AJAX呼び出しの後にコンポーネントがレンダリングされるという保証はありません。 – Rowland
コードのどの部分が非同期ですか?多くのGoogleマップの通話があり、そのいずれかであるかどうかは不明です。 –