@@ -5,6 +5,10 @@ import {ScriptCache} from './lib/ScriptCache';
55import GoogleApi from './lib/GoogleApi' ;
66
77const defaultMapConfig = { } ;
8+
9+ const serialize = obj => JSON . stringify ( obj ) ;
10+ const isSame = ( obj1 , obj2 ) => obj1 === obj2 || serialize ( obj1 ) === serialize ( obj2 ) ;
11+
812const defaultCreateCache = options => {
913 options = options || { } ;
1014 const apiKey = options . apiKey ;
@@ -30,19 +34,57 @@ export const wrapper = input => WrappedComponent => {
3034 class Wrapper extends React . Component {
3135 constructor ( props , context ) {
3236 super ( props , context ) ;
37+
38+ // Build options from input
3339 const options = typeof input === 'function' ? input ( props ) : input ;
40+
41+ // Initialize required Google scripts and other configured options
42+ this . initialize ( options ) ;
43+
44+ this . state = {
45+ loaded : false ,
46+ map : null ,
47+ google : null ,
48+ options : options
49+ } ;
50+ }
51+
52+ componentWillReceiveProps ( props ) {
53+ // Do not update input if it's not dynamic
54+ if ( typeof input !== 'function' ) {
55+ return ;
56+ }
57+
58+ // Get options to compare
59+ const prevOptions = this . state . options ;
60+ const options = typeof input === 'function' ? input ( props ) : input ;
61+
62+ // Ignore when options are not changed
63+ if ( isSame ( options , prevOptions ) ) {
64+ return ;
65+ }
66+
67+ // Initialize with new options
68+ this . initialize ( options ) ;
69+
70+ // Save new options in component state
71+ this . setState ( { options : options } ) ;
72+ }
73+
74+ initialize ( options ) {
75+ // Load cache factory
3476 const createCache = options . createCache || defaultCreateCache ;
3577
78+ // Build script
3679 this . scriptCache = createCache ( options ) ;
3780 this . scriptCache . google . onLoad ( this . onLoad . bind ( this ) ) ;
81+
82+ // Store information about loading container
3883 this . LoadingContainer =
3984 options . LoadingContainer || DefaultLoadingContainer ;
4085
41- this . state = {
42- loaded : false ,
43- map : null ,
44- google : null
45- } ;
86+ // Remove information about previous API handlers
87+ this . setState ( { loaded : false , google : null } ) ;
4688 }
4789
4890 onLoad ( err , tag ) {
0 commit comments