Skip to content

Commit

Permalink
Merge pull request #161 from yulinscottkang/v0/canvas
Browse files Browse the repository at this point in the history
fix: double canvas in react 18 strict mode
  • Loading branch information
yulinscottkang authored Jul 5, 2023
2 parents 7089bb9 + a64d271 commit 7249cec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-azure-maps",
"version": "0.4.7",
"version": "0.4.8",
"description": "React Wrapper for Azure Maps",
"keywords": [
"react",
Expand Down
22 changes: 15 additions & 7 deletions src/components/AzureMap/AzureMap.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { memo, useContext, useEffect, useState } from 'react'
import atlas from 'azure-maps-control'
import React, { memo, useContext, useEffect, useState, useRef } from 'react'
import atlas, { Map } from 'azure-maps-control'
import { IAzureMap, IAzureMapsContextProps, MapType } from '../../types'
import { AzureMapsContext } from '../../contexts/AzureMapContext'
import { Guid } from 'guid-typescript'
Expand Down Expand Up @@ -27,10 +27,15 @@ const AzureMap = memo(
styleOptions,
serviceOptions
}: IAzureMap) => {
const { setMapRef, removeMapRef, mapRef, setMapReady, isMapReady } = useContext<
IAzureMapsContextProps
>(AzureMapsContext)
const {
setMapRef,
removeMapRef,
mapRef,
setMapReady,
isMapReady
} = useContext<IAzureMapsContextProps>(AzureMapsContext)
const [mapId] = useState(providedMapId || Guid.create().toString())
const mapRefSource = useRef<Map | null>(null)
useEffect(() => {
if (mapRef) {
mapRef.setTraffic(trafficOptions)
Expand Down Expand Up @@ -61,7 +66,7 @@ const AzureMap = memo(
}
}, [serviceOptions])

useCheckRef<MapType, MapType>(mapRef, mapRef, mref => {
useCheckRef<MapType, MapType>(mapRef, mapRef, (mref) => {
mref.events.add('ready', () => {
if (imageSprites) {
createImageSprites(mref, imageSprites)
Expand Down Expand Up @@ -95,7 +100,10 @@ const AzureMap = memo(
})

useEffect(() => {
setMapRef(new atlas.Map(mapId, options))
if (mapRefSource.current === null) {
mapRefSource.current = new atlas.Map(mapId, options)
}
setMapRef(mapRefSource.current)
return () => {
removeMapRef()
}
Expand Down

0 comments on commit 7249cec

Please sign in to comment.