React throws this error when I delete a marker off the map. The usage is this: A marker component is placed on the map via new google.maps.Marker({...}), when it is clicked again, its latlng is sent to the reducer, which removes the marker from the Redux store. The marker is removed, but when that happens, I get the error. I read this comment on the React github page which says the error happens when "you’re trying to do DOM mutations by hand because it later tries to reconcile the DOM, and repeats what you’ve already done, causing failures." I not sure what's going on, but I think google-maps-react is trying to do this.
class GoogleMap extends Component {
constructor (props) {
super(props);
const {lat, lng} = this.props.initialCenter;
this.state = {
currentCenter: {
lat: lat,
lng: lng,
},
};
}
componentDidUpdate (prevProps, prevState) {
//check if props has been updated when app is first loaded
if (prevProps.google !== this.props.google) {
this.loadMap();
}
//check it a new marker has been added to redux store and passed down
if (prevProps.markers !== this.props.markers) {
this.getLatestMarker();
}
//check if state change when placing temp marker
if (prevState !== this.state) {
this.loadMap();
}
}
componentWillReceiveProps (nextProps) {
//when DELETE_MARKER is dispatched, re-load the map
if (this.props.markers !== nextProps.markers) {
this.loadMap();
}
}
loadMap () {
if (this.props && this.props.google) {
//if the google api has loaded into props
const {google} = this.props;
const maps = google.maps;
//reference to GoogleMap's div node
const mapRef = this.refs.map;
const node = ReactDOM.findDOMNode(mapRef);
let zoom = this.props.zoom; //zoom set via default props
//currentCenter set to default props initialCenter in state
const {lat, lng} = this.state.currentCenter;
const center = {lat, lng};
const mapConfig = {
center: center,
zoom: zoom,
styles: GoogleMapStyles,
};
this.map = new maps.Map(node, mapConfig);
//add listener for clicks on map to place markers
this.map.addListener('click', (e) => {
this.setTempMarker(e.latLng);
});
}
}
renderMarkers () {
if (this.props.markers) {
const markers = this.props.markers;
return Object.keys(markers).map(marker => {
return <Marker
key={marker}
google={this.props.google}
marker={markers[marker]}
map={this.map}
deleteMarker={this.props.deleteMarker}
/>;
});
}
}
render () {
return (
<div ref="map" className="map">
{this.renderMarkers()}
</div>
);
}
}
React throws this error when I delete a marker off the map. The usage is this: A marker component is placed on the map via new google.maps.Marker({...}), when it is clicked again, its latlng is sent to the reducer, which removes the marker from the Redux store. The marker is removed, but when that happens, I get the error. I read this comment on the React github page which says the error happens when "you’re trying to do DOM mutations by hand because it later tries to reconcile the DOM, and repeats what you’ve already done, causing failures." I not sure what's going on, but I think google-maps-react is trying to do this.
My code - App.js
GoogleMap.js - child of App
Marker.js - child of GoogleMap