Skip to content

Commit 30b44ee

Browse files
authored
Fix/map initialization (#16)
* fix(map): combine 'init' and 'mapContainer' hooks * refactor(map-hooks): remove redundant check
1 parent cab45eb commit 30b44ee

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

src/map-provider.tsx

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,37 +69,27 @@ const GoogleMapProvider = (props: GoogleMapProviderProps): JSX.Element => {
6969
new GoogleMap(mapOptions);
7070
};
7171

72-
// Initializes map on mount
73-
useEffect(() => {
74-
if (!mapContainer) {
75-
return (): void => {};
76-
}
77-
78-
// create new map instance
79-
createGoogleMap();
80-
81-
// Destroy Google Map when component unmounts
82-
return (): void => {
83-
map && map.destroyComplete();
84-
};
85-
}, []);
72+
// Destroy Google Map when component unmounts
73+
useEffect(() => map && map.destroyComplete(), []);
8674

8775
// Destroy and recreate map on mapcontainer change
8876
useEffect(() => {
89-
if (!map || !mapContainer) {
77+
if (!mapContainer) {
9078
return;
9179
}
9280

93-
// Destroy old map instance listeners
94-
map.destroyListeners();
81+
if (map) {
82+
// Destroy old map instance listeners
83+
map.destroyListeners();
84+
}
9585

9686
// create new map instance
9787
createGoogleMap();
9888
}, [mapContainer]);
9989

10090
// Destroy and recreate map on language or region change
10191
useEffect(() => {
102-
if (!map || !mapContainer) {
92+
if (!map) {
10393
return;
10494
}
10595

0 commit comments

Comments
 (0)