Skip to content

Commit 6a61f2c

Browse files
committed
fix(InfoWindow): use ReactDOM.createPortal in React@^16
* Closes #691
1 parent c4ded8b commit 6a61f2c

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/macros/InfoWindow.jsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* global google */
22
import invariant from "invariant"
3+
import canUseDOM from "can-use-dom"
34
import React from "react"
45
import ReactDOM from "react-dom"
56
import PropTypes from "prop-types"
@@ -52,8 +53,23 @@ export class InfoWindow extends React.PureComponent {
5253
}
5354
}
5455

56+
componentWillMount() {
57+
if (!canUseDOM || this.containerElement) {
58+
return
59+
}
60+
if (React.version.match(/^16/)) {
61+
this.containerElement = document.createElement(`div`)
62+
}
63+
}
64+
5565
componentDidMount() {
5666
componentDidMount(this, this.state[INFO_WINDOW], eventMap)
67+
if (React.version.match(/^16/)) {
68+
this.state[INFO_WINDOW].setContent(this.containerElement)
69+
open(this.state[INFO_WINDOW], this.context[ANCHOR])
70+
this.containerElement = undefined
71+
return
72+
}
5773
const content = document.createElement(`div`)
5874
ReactDOM.unstable_renderSubtreeIntoContainer(
5975
this,
@@ -72,6 +88,9 @@ export class InfoWindow extends React.PureComponent {
7288
updaterMap,
7389
prevProps
7490
)
91+
if (React.version.match(/^16/)) {
92+
return
93+
}
7594
if (this.props.children !== prevProps.children) {
7695
ReactDOM.unstable_renderSubtreeIntoContainer(
7796
this,
@@ -85,14 +104,20 @@ export class InfoWindow extends React.PureComponent {
85104
componentWillUnmount(this)
86105
const infoWindow = this.state[INFO_WINDOW]
87106
if (infoWindow) {
88-
if (infoWindow.getContent()) {
107+
if (!React.version.match(/^16/) && infoWindow.getContent()) {
89108
ReactDOM.unmountComponentAtNode(infoWindow.getContent())
90109
}
91110
infoWindow.setMap(null)
92111
}
93112
}
94113

95114
render() {
115+
if (React.version.match(/^16/)) {
116+
return ReactDOM.createPortal(
117+
React.Children.only(this.props.children),
118+
this.containerElement
119+
)
120+
}
96121
return false
97122
}
98123
}

0 commit comments

Comments
 (0)