From 059dfb7371dcd10dbce97f28c869ee94067a030b Mon Sep 17 00:00:00 2001 From: Ryan Hefner Date: Sun, 25 Aug 2019 13:36:49 -0400 Subject: [PATCH] Wrap all window references in type check for SSR useage --- src/GoogleMap.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/GoogleMap.js b/src/GoogleMap.js index f87359dd..6cb17986 100644 --- a/src/GoogleMap.js +++ b/src/GoogleMap.js @@ -36,16 +36,18 @@ const CALLBACK_MAP = { 'zoom_changed': 'onZoomChange', }; -window['reactMapsGoogleInstances'] = []; -window['reactMapsGoogleInit'] = () => { - window['reactMapsGoogleInstances'].forEach(instance => instance()); -}; +if (typeof window !== 'undefined') { + window['reactMapsGoogleInstances'] = []; + window['reactMapsGoogleInit'] = () => { + window['reactMapsGoogleInstances'].forEach(instance => instance()); + }; +} class GoogleMap extends Component { constructor(props) { super(props); - const scriptLoaded = window.google && window.google.maps && window.google.maps.Map + const scriptLoaded = typeof window !== 'undefined' && window.google && window.google.maps && window.google.maps.Map ? true : false; @@ -56,11 +58,13 @@ class GoogleMap extends Component { this.onScriptLoad = this.onScriptLoad.bind(this); this.onScriptInit = this.onScriptInit.bind(this); - - window['reactMapsGoogleInstances'].push(this.onScriptInit); } componentDidMount() { + if (typeof window !== 'undefined') { + window['reactMapsGoogleInstances'].push(this.onScriptInit); + } + this.renderMap(); }