javascript - React - Google Map component not change map after re-render - Stack Overflow

I have a problem with Google Map in React.Everything works great but, when I try to re-render other pl

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
Add a ment  | 

1 Answer 1

Reset to default 6

You 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信