Skip to content

Commit 40fe3ea

Browse files
KevinRamsunderitsmichaeldiego
authored andcommitted
Add shouldUnregisterMapOnUnmount prop to persist map object on unmount (google-map-react#722)
1 parent c8a7f56 commit 40fe3ea

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/google_map.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export default class GoogleMap extends Component {
137137
style: PropTypes.any,
138138
resetBoundsOnResize: PropTypes.bool,
139139
layerTypes: PropTypes.arrayOf(PropTypes.string), // ['TransitLayer', 'TrafficLayer']
140+
shouldUnregisterMapOnUnmount: PropTypes.bool,
140141
};
141142

142143
static defaultProps = {
@@ -161,6 +162,7 @@ export default class GoogleMap extends Component {
161162
layerTypes: [],
162163
heatmap: {},
163164
heatmapLibrary: false,
165+
shouldUnregisterMapOnUnmount: true,
164166
};
165167

166168
static googleMapLoader = googleMapLoader; // eslint-disable-line
@@ -424,20 +426,24 @@ export default class GoogleMap extends Component {
424426
this.overlay_.setMap(null);
425427
}
426428

427-
if (this.maps_ && this.map_) {
429+
if (this.maps_ && this.map_ && this.props.shouldUnregisterMapOnUnmount) {
428430
// fix google, as otherwise listeners works even without map
429431
this.map_.setOptions({ scrollwheel: false });
430432
this.maps_.event.clearInstanceListeners(this.map_);
431433
}
432434

433-
this.map_ = null;
434-
this.maps_ = null;
435+
if (this.props.shouldUnregisterMapOnUnmount) {
436+
this.map_ = null;
437+
this.maps_ = null;
438+
}
435439
this.markersDispatcher_.dispose();
436440

437441
this.resetSizeOnIdle_ = false;
438442

439-
delete this.map_;
440-
delete this.markersDispatcher_;
443+
if (this.props.shouldUnregisterMapOnUnmount) {
444+
delete this.map_;
445+
delete this.markersDispatcher_;
446+
}
441447
}
442448
// calc minZoom if map size available
443449
// it's better to not set minZoom less than this calculation gives
@@ -662,7 +668,7 @@ export default class GoogleMap extends Component {
662668
this_._onBoundsChanged(map, maps, !this_.props.debounced);
663669

664670
if (!this_.googleApiLoadedCalled_) {
665-
this_._onGoogleApiLoaded({ map, maps });
671+
this_._onGoogleApiLoaded({ map, maps, ref: this_.googleMapDom_ });
666672
this_.googleApiLoadedCalled_ = true;
667673
}
668674

@@ -794,7 +800,11 @@ export default class GoogleMap extends Component {
794800
})
795801
.catch(e => {
796802
// notify callback of load failure
797-
this._onGoogleApiLoaded({ map: null, maps: null });
803+
this._onGoogleApiLoaded({
804+
map: null,
805+
maps: null,
806+
ref: this.googleMapDom_,
807+
});
798808
console.error(e); // eslint-disable-line no-console
799809
throw e;
800810
});

0 commit comments

Comments
 (0)