I have a map application that I built for which some map icons appear / disappear after clicking a button, but I canβt figure out how to configure it to re-render the component when I pass a new sports property from this parent component:
Parental component:
<SimpleMap sports=[default value and future values go here] />
Simple card component (simplified):
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) => { this.setState({events: response}); } }; componentDidMount () { this.makeMapEvents(this.state.sports); console.log("mounted", this.state.sports); }
In the end, it ends here to display events from a state:
<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>