Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: double canvas in react 18 strict mode #161

Merged
merged 2 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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