Skip to content

Commit 7249cec

Browse files
Merge pull request #161 from yulinscottkang/v0/canvas
fix: double canvas in react 18 strict mode
2 parents 7089bb9 + a64d271 commit 7249cec

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-azure-maps",
3-
"version": "0.4.7",
3+
"version": "0.4.8",
44
"description": "React Wrapper for Azure Maps",
55
"keywords": [
66
"react",

src/components/AzureMap/AzureMap.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { memo, useContext, useEffect, useState } from 'react'
2-
import atlas from 'azure-maps-control'
1+
import React, { memo, useContext, useEffect, useState, useRef } from 'react'
2+
import atlas, { Map } from 'azure-maps-control'
33
import { IAzureMap, IAzureMapsContextProps, MapType } from '../../types'
44
import { AzureMapsContext } from '../../contexts/AzureMapContext'
55
import { Guid } from 'guid-typescript'
@@ -27,10 +27,15 @@ const AzureMap = memo(
2727
styleOptions,
2828
serviceOptions
2929
}: IAzureMap) => {
30-
const { setMapRef, removeMapRef, mapRef, setMapReady, isMapReady } = useContext<
31-
IAzureMapsContextProps
32-
>(AzureMapsContext)
30+
const {
31+
setMapRef,
32+
removeMapRef,
33+
mapRef,
34+
setMapReady,
35+
isMapReady
36+
} = useContext<IAzureMapsContextProps>(AzureMapsContext)
3337
const [mapId] = useState(providedMapId || Guid.create().toString())
38+
const mapRefSource = useRef<Map | null>(null)
3439
useEffect(() => {
3540
if (mapRef) {
3641
mapRef.setTraffic(trafficOptions)
@@ -61,7 +66,7 @@ const AzureMap = memo(
6166
}
6267
}, [serviceOptions])
6368

64-
useCheckRef<MapType, MapType>(mapRef, mapRef, mref => {
69+
useCheckRef<MapType, MapType>(mapRef, mapRef, (mref) => {
6570
mref.events.add('ready', () => {
6671
if (imageSprites) {
6772
createImageSprites(mref, imageSprites)
@@ -95,7 +100,10 @@ const AzureMap = memo(
95100
})
96101

97102
useEffect(() => {
98-
setMapRef(new atlas.Map(mapId, options))
103+
if (mapRefSource.current === null) {
104+
mapRefSource.current = new atlas.Map(mapId, options)
105+
}
106+
setMapRef(mapRefSource.current)
99107
return () => {
100108
removeMapRef()
101109
}

0 commit comments

Comments
 (0)