@@ -301,81 +301,90 @@ class GoogleMap extends Component {
301
301
}
302
302
}
303
303
304
- // eslint-disable-next-line camelcase
305
- UNSAFE_componentWillReceiveProps ( nextProps ) {
304
+ shouldComponentUpdate ( nextProps , nextState ) {
305
+ // draggable does not affect inner components
306
+ return (
307
+ ! shallowEqual (
308
+ omit ( this . props , [ 'draggable' ] ) ,
309
+ omit ( nextProps , [ 'draggable' ] )
310
+ ) || ! shallowEqual ( this . state , nextState )
311
+ ) ;
312
+ }
313
+
314
+ componentDidUpdate ( prevProps ) {
306
315
if ( process . env . NODE_ENV !== 'production' ) {
307
- if ( ! shallowEqual ( this . props . defaultCenter , nextProps . defaultCenter ) ) {
316
+ if ( ! shallowEqual ( prevProps . defaultCenter , this . props . defaultCenter ) ) {
308
317
console . warn (
309
318
"GoogleMap: defaultCenter prop changed. You can't change default props."
310
319
) ;
311
320
}
312
321
313
- if ( ! shallowEqual ( this . props . defaultZoom , nextProps . defaultZoom ) ) {
322
+ if ( ! shallowEqual ( prevProps . defaultZoom , this . props . defaultZoom ) ) {
314
323
console . warn (
315
324
"GoogleMap: defaultZoom prop changed. You can't change default props."
316
325
) ;
317
326
}
318
327
}
319
328
320
329
if (
321
- ! this . _isCenterDefined ( this . props . center ) &&
322
- this . _isCenterDefined ( nextProps . center )
330
+ ! this . _isCenterDefined ( prevProps . center ) &&
331
+ this . _isCenterDefined ( this . props . center )
323
332
) {
324
333
setTimeout ( ( ) => this . _initMap ( ) , 0 ) ;
325
334
}
326
335
327
336
if ( this . map_ ) {
328
337
const centerLatLng = this . geoService_ . getCenter ( ) ;
329
- if ( this . _isCenterDefined ( nextProps . center ) ) {
330
- const nextPropsCenter = latLng2Obj ( nextProps . center ) ;
331
- const currCenter = this . _isCenterDefined ( this . props . center )
332
- ? latLng2Obj ( this . props . center )
338
+ if ( this . _isCenterDefined ( this . props . center ) ) {
339
+ const currentCenter = latLng2Obj ( this . props . center ) ;
340
+ const prevCenter = this . _isCenterDefined ( prevProps . center )
341
+ ? latLng2Obj ( prevProps . center )
333
342
: null ;
334
343
335
344
if (
336
- ! currCenter ||
337
- Math . abs ( nextPropsCenter . lat - currCenter . lat ) +
338
- Math . abs ( nextPropsCenter . lng - currCenter . lng ) >
345
+ ! prevCenter ||
346
+ Math . abs ( currentCenter . lat - prevCenter . lat ) +
347
+ Math . abs ( currentCenter . lng - prevCenter . lng ) >
339
348
kEPS
340
349
) {
341
350
if (
342
- Math . abs ( nextPropsCenter . lat - centerLatLng . lat ) +
343
- Math . abs ( nextPropsCenter . lng - centerLatLng . lng ) >
351
+ Math . abs ( currentCenter . lat - centerLatLng . lat ) +
352
+ Math . abs ( currentCenter . lng - centerLatLng . lng ) >
344
353
kEPS
345
354
) {
346
355
this . map_ . panTo ( {
347
- lat : nextPropsCenter . lat ,
348
- lng : nextPropsCenter . lng ,
356
+ lat : currentCenter . lat ,
357
+ lng : currentCenter . lng ,
349
358
} ) ;
350
359
}
351
360
}
352
361
}
353
362
354
- if ( ! isEmpty ( nextProps . zoom ) ) {
363
+ if ( ! isEmpty ( this . props . zoom ) ) {
355
364
// if zoom chaged by user
356
- if ( Math . abs ( nextProps . zoom - this . props . zoom ) > 0 ) {
357
- this . map_ . setZoom ( nextProps . zoom ) ;
365
+ if ( Math . abs ( this . props . zoom - prevProps . zoom ) > 0 ) {
366
+ this . map_ . setZoom ( this . props . zoom ) ;
358
367
}
359
368
}
360
369
361
- if ( ! isEmpty ( this . props . draggable ) && isEmpty ( nextProps . draggable ) ) {
370
+ if ( ! isEmpty ( prevProps . draggable ) && isEmpty ( this . props . draggable ) ) {
362
371
// reset to default
363
372
this . map_ . setOptions ( { draggable : this . defaultDraggableOption_ } ) ;
364
- } else if ( ! shallowEqual ( this . props . draggable , nextProps . draggable ) ) {
373
+ } else if ( ! shallowEqual ( prevProps . draggable , this . props . draggable ) ) {
365
374
// also prevent this on window 'mousedown' event to prevent map move
366
- this . map_ . setOptions ( { draggable : nextProps . draggable } ) ;
375
+ this . map_ . setOptions ( { draggable : this . props . draggable } ) ;
367
376
}
368
377
369
378
// use shallowEqual to try avoid calling map._setOptions if only the ref changes
370
379
if (
371
- ! isEmpty ( nextProps . options ) &&
372
- ! shallowEqual ( this . props . options , nextProps . options )
380
+ ! isEmpty ( this . props . options ) &&
381
+ ! shallowEqual ( prevProps . options , this . props . options )
373
382
) {
374
383
const mapPlainObjects = pick ( this . maps_ , isPlainObject ) ;
375
384
let options =
376
- typeof nextProps . options === 'function'
377
- ? nextProps . options ( mapPlainObjects )
378
- : nextProps . options ;
385
+ typeof this . props . options === 'function'
386
+ ? this . props . options ( mapPlainObjects )
387
+ : this . props . options ;
379
388
// remove zoom, center and draggable options as these are managed by google-maps-react
380
389
options = omit ( options , [ 'zoom' , 'center' , 'draggable' ] ) ;
381
390
@@ -387,47 +396,35 @@ class GoogleMap extends Component {
387
396
this . map_ . setOptions ( options ) ;
388
397
}
389
398
390
- if ( ! shallowEqual ( nextProps . layerTypes , this . props . layerTypes ) ) {
399
+ if ( ! shallowEqual ( this . props . layerTypes , prevProps . layerTypes ) ) {
391
400
Object . keys ( this . layers_ ) . forEach ( ( layerKey ) => {
392
401
this . layers_ [ layerKey ] . setMap ( null ) ;
393
402
delete this . layers_ [ layerKey ] ;
394
403
} ) ;
395
- this . _setLayers ( nextProps . layerTypes ) ;
404
+ this . _setLayers ( this . props . layerTypes ) ;
396
405
}
397
406
398
407
if (
399
408
this . heatmap &&
400
- ! shallowEqual ( nextProps . heatmap . positions , this . props . heatmap . positions )
409
+ ! shallowEqual ( this . props . heatmap . positions , prevProps . heatmap . positions )
401
410
) {
402
411
this . heatmap . setData (
403
- nextProps . heatmap . positions . map ( ( p ) => ( {
412
+ this . props . heatmap . positions . map ( ( p ) => ( {
404
413
location : new this . maps_ . LatLng ( p . lat , p . lng ) ,
405
414
weight : p . weight ,
406
415
} ) )
407
416
) ;
408
417
}
409
418
if (
410
419
this . heatmap &&
411
- ! shallowEqual ( nextProps . heatmap . options , this . props . heatmap . options )
420
+ ! shallowEqual ( this . props . heatmap . options , prevProps . heatmap . options )
412
421
) {
413
- Object . keys ( nextProps . heatmap . options ) . forEach ( ( option ) => {
414
- this . heatmap . set ( option , nextProps . heatmap . options [ option ] ) ;
422
+ Object . keys ( this . props . heatmap . options ) . forEach ( ( option ) => {
423
+ this . heatmap . set ( option , this . props . heatmap . options [ option ] ) ;
415
424
} ) ;
416
425
}
417
426
}
418
- }
419
-
420
- shouldComponentUpdate ( nextProps , nextState ) {
421
- // draggable does not affect inner components
422
- return (
423
- ! shallowEqual (
424
- omit ( this . props , [ 'draggable' ] ) ,
425
- omit ( nextProps , [ 'draggable' ] )
426
- ) || ! shallowEqual ( this . state , nextState )
427
- ) ;
428
- }
429
-
430
- componentDidUpdate ( prevProps ) {
427
+ // emit actions
431
428
this . markersDispatcher_ . emit ( 'kON_CHANGE' ) ;
432
429
433
430
if ( ! shallowEqual ( this . props . hoverDistance , prevProps . hoverDistance ) ) {
0 commit comments