Skip to content

Commit

Permalink
Merge pull request #162 from yulinscottkang/v1/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 3972727 + 37e8b46 commit e1d9f88
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 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": "1.0.0-beta.2",
"version": "1.0.0-beta.3",
"description": "React Wrapper for Azure Maps",
"keywords": [
"react",
Expand Down
10 changes: 7 additions & 3 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 @@ -33,6 +33,7 @@ const AzureMap = memo(
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 @@ -97,7 +98,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 e1d9f88

Please sign in to comment.