From 6a61f2c4e2c6dc1956b3154fb2ad90795842371b Mon Sep 17 00:00:00 2001 From: Tom Chen Date: Fri, 3 Nov 2017 15:41:58 +0800 Subject: [PATCH] fix(InfoWindow): use `ReactDOM.createPortal` in React@^16 * Closes #691 --- src/macros/InfoWindow.jsx | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/macros/InfoWindow.jsx b/src/macros/InfoWindow.jsx index 94e2f66a..b368c154 100644 --- a/src/macros/InfoWindow.jsx +++ b/src/macros/InfoWindow.jsx @@ -1,5 +1,6 @@ /* global google */ import invariant from "invariant" +import canUseDOM from "can-use-dom" import React from "react" import ReactDOM from "react-dom" import PropTypes from "prop-types" @@ -52,8 +53,23 @@ export class InfoWindow extends React.PureComponent { } } + componentWillMount() { + if (!canUseDOM || this.containerElement) { + return + } + if (React.version.match(/^16/)) { + this.containerElement = document.createElement(`div`) + } + } + componentDidMount() { componentDidMount(this, this.state[INFO_WINDOW], eventMap) + if (React.version.match(/^16/)) { + this.state[INFO_WINDOW].setContent(this.containerElement) + open(this.state[INFO_WINDOW], this.context[ANCHOR]) + this.containerElement = undefined + return + } const content = document.createElement(`div`) ReactDOM.unstable_renderSubtreeIntoContainer( this, @@ -72,6 +88,9 @@ export class InfoWindow extends React.PureComponent { updaterMap, prevProps ) + if (React.version.match(/^16/)) { + return + } if (this.props.children !== prevProps.children) { ReactDOM.unstable_renderSubtreeIntoContainer( this, @@ -85,7 +104,7 @@ export class InfoWindow extends React.PureComponent { componentWillUnmount(this) const infoWindow = this.state[INFO_WINDOW] if (infoWindow) { - if (infoWindow.getContent()) { + if (!React.version.match(/^16/) && infoWindow.getContent()) { ReactDOM.unmountComponentAtNode(infoWindow.getContent()) } infoWindow.setMap(null) @@ -93,6 +112,12 @@ export class InfoWindow extends React.PureComponent { } render() { + if (React.version.match(/^16/)) { + return ReactDOM.createPortal( + React.Children.only(this.props.children), + this.containerElement + ) + } return false } }