我有一个我构建的地图应用程序,它需要一些地图图标在按下按钮后出现/消失,但是当我从它的父组件传入一个新的运动属性时,我不知道如何设置它以重新渲染组件:
父加载组件:
<SimpleMap sports=[default value and future values go here] />
简单地图组件(简化版):
constructor(props) {
(props);
this.state = {
events: [{venue: {lat: 2, lon: 1}}],
sports: ["baseball", "football", "paddle", "soccer", "boxing", "dart", "biking", "golf", "hockey", "inline-skating", "tennis", "volleyball", "skateboard", "kickball", "bowling", "pool", "ride", "hike", "ice-skating"]
};
};
componentWillReceiveProps (nextProps) {
this.setState({events: [{venue: {lat: 2, lon: 1}}],
sports: nextProps.sports});
console.log(nextProps.sports);
}
static defaultProps = {
center: {lat: 36.160338, lng: -86.778780},
zoom: 12,
sports: ["baseball", "football", "paddle", "soccer", "boxing", "dart", "biking", "golf", "hockey", "inline-skating", "tennis", "volleyball", "skateboard", "kickball", "bowling", "pool", "ride", "hike", "ice-skating"],
};
makeMapEvents (insertProps) {
fetch("./json/meetup.json").then((response) => {
return response.json()
}).then((response) => {
/* eventually returns new events object based on insertProps */
this.setState({events: response});
}
};
componentDidMount () {
this.makeMapEvents(this.state.sports);
console.log("mounted", this.state.sports);
}
最终在这里结束从状态映射事件:
<GoogleMap>
{this.state.events.map((result) => {
return (<Marker key={counter++} lat={result.venue.lat} lng={result.venue.lon}
sport={this.props.sport} data={result}/>);
})}
</GoogleMap>