I have a problem with Google Map in React. Everything works great but, when I try to re-render other place in this map it doesnt work :( I mean when I change city (e.g. select) new props (latitude, longitude) are used , but city one the map not change
import React from 'react';
import ReactDOM from 'react-dom';
import { withGoogleMap, GoogleMap, Marker } from "react-google-maps";
const SimpleMapExampleGoogleMap = withGoogleMap( props => {
console.log("here new props are used", props)
return <GoogleMap
defaultZoom={15}
defaultCenter={new google.maps.LatLng(props.lat, props.lng)}
/>
}
);
class GMap extends React.Component{
constructor(props) {
super(props);
this.state = {
lat: this.props.lat,
lng: this.props.lng
}
}
render() {
console.log("New props ", this.props)
return <SimpleMapExampleGoogleMap
lat={this.props.lat}
lng={this.props.lng}
containerElement={
<div style={{ height: `500px` }} />
}
mapElement={
<div style={{ height: `500px` }} />
}
/>
}
}
export { GMap }
I have a problem with Google Map in React. Everything works great but, when I try to re-render other place in this map it doesnt work :( I mean when I change city (e.g. select) new props (latitude, longitude) are used , but city one the map not change
import React from 'react';
import ReactDOM from 'react-dom';
import { withGoogleMap, GoogleMap, Marker } from "react-google-maps";
const SimpleMapExampleGoogleMap = withGoogleMap( props => {
console.log("here new props are used", props)
return <GoogleMap
defaultZoom={15}
defaultCenter={new google.maps.LatLng(props.lat, props.lng)}
/>
}
);
class GMap extends React.Component{
constructor(props) {
super(props);
this.state = {
lat: this.props.lat,
lng: this.props.lng
}
}
render() {
console.log("New props ", this.props)
return <SimpleMapExampleGoogleMap
lat={this.props.lat}
lng={this.props.lng}
containerElement={
<div style={{ height: `500px` }} />
}
mapElement={
<div style={{ height: `500px` }} />
}
/>
}
}
export { GMap }
Share
Improve this question
asked May 23, 2017 at 7:24
AgataAgata
5541 gold badge9 silver badges18 bronze badges
1 Answer
Reset to default 6You have set state for lng
and lat
but you didn't apply them on the map
=> change this.props.lat
to this.state.lat
, and same for the lng
:
return <SimpleMapExampleGoogleMap
lat={this.state.lat}
lng={this.state.lng}
containerElement={
<div style={{ height: `500px` }} />
}
mapElement={
<div style={{ height: `500px` }} />
}
/>
If the above code doesn't update your map yet, then feel free to add the following method (same level as constructors):
constructors(props){/*...*/}
ponentWillReceiveProps(nextProps) {
this.setState({
lat: nextProps.lat,
lng: nextProps.lng,
});
}
render(){/*...*/}
Please also post here if you have errors, thanks
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744598993a4583065.html
评论列表(0条)