Skip to content

Commit b121bb6

Browse files
clayne11itsmichaeldiego
authored andcommitted
Use React 16 portal to render map overlay (google-map-react#643)
* Use React 16 portal to render map overlay This allows the new context API to propagate context properly. * Rename div to overlay
1 parent 28506dd commit b121bb6

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

src/google_map.js

+31-23
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export default class GoogleMap extends Component {
239239
this.zoomAnimationInProgress_ = false;
240240

241241
this.state = {
242-
overlayCreated: false,
242+
overlay: null,
243243
};
244244
}
245245

@@ -587,7 +587,7 @@ export default class GoogleMap extends Component {
587587
: '2000px';
588588

589589
const div = document.createElement('div');
590-
this.div = div;
590+
this.overlay = div;
591591
div.style.backgroundColor = 'transparent';
592592
div.style.position = 'absolute';
593593
div.style.left = '0px';
@@ -601,29 +601,21 @@ export default class GoogleMap extends Component {
601601
maps,
602602
overlay.getProjection()
603603
);
604-
ReactDOM.unstable_renderSubtreeIntoContainer(
605-
this_,
606-
<GoogleMapMarkers
607-
experimental={this_.props.experimental}
608-
onChildClick={this_._onChildClick}
609-
onChildMouseDown={this_._onChildMouseDown}
610-
onChildMouseEnter={this_._onChildMouseEnter}
611-
onChildMouseLeave={this_._onChildMouseLeave}
612-
geoService={this_.geoService_}
613-
insideMapPanes
614-
distanceToMouse={this_.props.distanceToMouse}
615-
getHoverDistance={this_._getHoverDistance}
616-
dispatcher={this_.markersDispatcher_}
617-
/>,
618-
div,
619-
// remove prerendered markers
620-
() => this_.setState({ overlayCreated: true })
621-
);
604+
605+
this_.setState(state => ({
606+
...state,
607+
overlay: div,
608+
}));
622609
},
623610

624611
onRemove() {
625-
if (this.div) {
626-
ReactDOM.unmountComponentAtNode(this.div);
612+
this_.setState(state => ({
613+
...state,
614+
overlay: null,
615+
}));
616+
617+
if (this.overlay) {
618+
ReactDOM.unmountComponentAtNode(this.overlay);
627619
}
628620
},
629621

@@ -1062,7 +1054,7 @@ export default class GoogleMap extends Component {
10621054
};
10631055

10641056
render() {
1065-
const mapMarkerPrerender = !this.state.overlayCreated
1057+
const mapMarkerPrerender = !this.state.overlay
10661058
? <GoogleMapMarkersPrerender
10671059
experimental={this.props.experimental}
10681060
onChildClick={this._onChildClick}
@@ -1085,6 +1077,22 @@ export default class GoogleMap extends Component {
10851077
onClick={this._onMapClick}
10861078
>
10871079
<GoogleMapMap registerChild={this._registerChild} />
1080+
{this.state.overlay &&
1081+
ReactDOM.createPortal(
1082+
<GoogleMapMarkers
1083+
experimental={this.props.experimental}
1084+
onChildClick={this._onChildClick}
1085+
onChildMouseDown={this._onChildMouseDown}
1086+
onChildMouseEnter={this._onChildMouseEnter}
1087+
onChildMouseLeave={this._onChildMouseLeave}
1088+
geoService={this.geoService_}
1089+
insideMapPanes
1090+
distanceToMouse={this.props.distanceToMouse}
1091+
getHoverDistance={this._getHoverDistance}
1092+
dispatcher={this.markersDispatcher_}
1093+
/>,
1094+
this.state.overlay
1095+
)}
10881096

10891097
{/* render markers before map load done */}
10901098
{mapMarkerPrerender}

0 commit comments

Comments
 (0)