Skip to content

Commit f851127

Browse files
committed
✨ Add componentDidUpdate for PositionInput
1 parent a7d59bd commit f851127

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/react-chayns-position_input/component/GoogleMap/GoogleMap.jsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import React, { PureComponent, Children, cloneElement, createRef } from 'react';
55
import PropTypes from 'prop-types';
66

77
import { toPropTypes, toHandlerName } from './PropTypes';
8+
import deepEqual from '../../../utils/deepEqual';
89

910
const events = [
1011
'bounds_changed',
@@ -61,6 +62,14 @@ class GoogleMap extends PureComponent {
6162
}
6263
}
6364

65+
componentDidUpdate(prevProps) {
66+
const { options } = this.props;
67+
68+
if (!deepEqual(prevProps.options, options)) {
69+
this.map.setOptions(options);
70+
}
71+
}
72+
6473
/** Passes the map instance down to children */
6574
renderChildren() {
6675
const { children } = this.props;

src/utils/deepEqual.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Compares if two objects are deeply equal
3+
* @param obj1
4+
* @param obj2
5+
* @returns {boolean|this is string[]}
6+
*/
7+
const deepEqual = (obj1, obj2) => {
8+
if (obj1 === obj2) return true;
9+
10+
if (!(typeof obj1 === 'object')) return false;
11+
if (!(typeof obj2 === 'object')) return false;
12+
13+
if (obj1 === null) return false;
14+
if (obj2 === null) return false;
15+
16+
if (Object.keys(obj1).length !== Object.keys(obj2).length) return false;
17+
18+
return Object.keys(obj1).every((key) => deepEqual(obj1[key], obj2[key]));
19+
}
20+
21+
export default deepEqual;

0 commit comments

Comments
 (0)