Skip to content

Commit aa10167

Browse files
Change componentWillReceiveProps to componentDidUpdate (google-map-react#950)
1 parent af735e7 commit aa10167

File tree

1 file changed

+45
-48
lines changed

1 file changed

+45
-48
lines changed

src/google_map.js

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -301,81 +301,90 @@ class GoogleMap extends Component {
301301
}
302302
}
303303

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) {
306315
if (process.env.NODE_ENV !== 'production') {
307-
if (!shallowEqual(this.props.defaultCenter, nextProps.defaultCenter)) {
316+
if (!shallowEqual(prevProps.defaultCenter, this.props.defaultCenter)) {
308317
console.warn(
309318
"GoogleMap: defaultCenter prop changed. You can't change default props."
310319
);
311320
}
312321

313-
if (!shallowEqual(this.props.defaultZoom, nextProps.defaultZoom)) {
322+
if (!shallowEqual(prevProps.defaultZoom, this.props.defaultZoom)) {
314323
console.warn(
315324
"GoogleMap: defaultZoom prop changed. You can't change default props."
316325
);
317326
}
318327
}
319328

320329
if (
321-
!this._isCenterDefined(this.props.center) &&
322-
this._isCenterDefined(nextProps.center)
330+
!this._isCenterDefined(prevProps.center) &&
331+
this._isCenterDefined(this.props.center)
323332
) {
324333
setTimeout(() => this._initMap(), 0);
325334
}
326335

327336
if (this.map_) {
328337
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)
333342
: null;
334343

335344
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) >
339348
kEPS
340349
) {
341350
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) >
344353
kEPS
345354
) {
346355
this.map_.panTo({
347-
lat: nextPropsCenter.lat,
348-
lng: nextPropsCenter.lng,
356+
lat: currentCenter.lat,
357+
lng: currentCenter.lng,
349358
});
350359
}
351360
}
352361
}
353362

354-
if (!isEmpty(nextProps.zoom)) {
363+
if (!isEmpty(this.props.zoom)) {
355364
// 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);
358367
}
359368
}
360369

361-
if (!isEmpty(this.props.draggable) && isEmpty(nextProps.draggable)) {
370+
if (!isEmpty(prevProps.draggable) && isEmpty(this.props.draggable)) {
362371
// reset to default
363372
this.map_.setOptions({ draggable: this.defaultDraggableOption_ });
364-
} else if (!shallowEqual(this.props.draggable, nextProps.draggable)) {
373+
} else if (!shallowEqual(prevProps.draggable, this.props.draggable)) {
365374
// 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 });
367376
}
368377

369378
// use shallowEqual to try avoid calling map._setOptions if only the ref changes
370379
if (
371-
!isEmpty(nextProps.options) &&
372-
!shallowEqual(this.props.options, nextProps.options)
380+
!isEmpty(this.props.options) &&
381+
!shallowEqual(prevProps.options, this.props.options)
373382
) {
374383
const mapPlainObjects = pick(this.maps_, isPlainObject);
375384
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;
379388
// remove zoom, center and draggable options as these are managed by google-maps-react
380389
options = omit(options, ['zoom', 'center', 'draggable']);
381390

@@ -387,47 +396,35 @@ class GoogleMap extends Component {
387396
this.map_.setOptions(options);
388397
}
389398

390-
if (!shallowEqual(nextProps.layerTypes, this.props.layerTypes)) {
399+
if (!shallowEqual(this.props.layerTypes, prevProps.layerTypes)) {
391400
Object.keys(this.layers_).forEach((layerKey) => {
392401
this.layers_[layerKey].setMap(null);
393402
delete this.layers_[layerKey];
394403
});
395-
this._setLayers(nextProps.layerTypes);
404+
this._setLayers(this.props.layerTypes);
396405
}
397406

398407
if (
399408
this.heatmap &&
400-
!shallowEqual(nextProps.heatmap.positions, this.props.heatmap.positions)
409+
!shallowEqual(this.props.heatmap.positions, prevProps.heatmap.positions)
401410
) {
402411
this.heatmap.setData(
403-
nextProps.heatmap.positions.map((p) => ({
412+
this.props.heatmap.positions.map((p) => ({
404413
location: new this.maps_.LatLng(p.lat, p.lng),
405414
weight: p.weight,
406415
}))
407416
);
408417
}
409418
if (
410419
this.heatmap &&
411-
!shallowEqual(nextProps.heatmap.options, this.props.heatmap.options)
420+
!shallowEqual(this.props.heatmap.options, prevProps.heatmap.options)
412421
) {
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]);
415424
});
416425
}
417426
}
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
431428
this.markersDispatcher_.emit('kON_CHANGE');
432429

433430
if (!shallowEqual(this.props.hoverDistance, prevProps.hoverDistance)) {

0 commit comments

Comments
 (0)