【发布时间】:2017-02-09 09:06:03
【问题描述】:
所以我有大量从 API 检索的数据。我认为问题在于我的组件在从 Promise 接收到数据之前调用了 renderMarkers 函数。
所以我想知道如何在调用我的 renderMarkers 函数之前等待完全解析数据的承诺?
class Map extends Component {
componentDidMount() {
console.log(this.props)
new google.maps.Map(this.refs.map, {
zoom: 12,
center: {
lat: this.props.route.lat,
lng: this.props.route.lng
}
})
}
componentWillMount() {
this.props.fetchWells()
}
renderMarkers() {
return this.props.wells.map((wells) => {
console.log(wells)
})
}
render() {
return (
<div id="map" ref="map">
{this.renderMarkers()}
</div>
)
}
}
function mapStateToProps(state) {
return { wells: state.wells.all };
}
export default connect(mapStateToProps, { fetchWells })(Map);
【问题讨论】:
-
在
componentDidMount生命周期方法中获取服务器数据。看看 Dan Abramov 的 this tweet -
谢谢,我把它改了,但是在数据返回之前组件仍在调用渲染函数
标签: javascript reactjs