Markers don't update location correctly according to state
See original GitHub issueThe marker location doesn’t update with state correctly the first time its changed, instead a third marker is created even though there’re only two in state, if I do another setTimeout to update it, it updates the newly created one correctly. It seems like the library is caching & not removing the old markers on initial render or something similar.
import React from 'react'
import { withScriptjs, withGoogleMap, GoogleMap, Marker } from 'react-google-maps'
class Map extends React.PureComponent {
constructor (props) {
super(props)
this.state = {
demoData: {
'device:58fe8b4b-befd-4d03-837c-925fa5b61476': {
'latitude': 52.51133948867925,
'longitude': 13.406048111320755
},
'fake': {
'latitude': 52.51115736666667,
'longitude': 13.465472335185186
}
}
}
this.mapOptions = {
zoomControl: true,
mapTypeControl: false,
scaleControl: true,
streetViewControl: false,
rotateControl: false,
fullscreenControl: false
}
}
componentDidMount () {
setTimeout(() => {
this.setState({ demoData: { ...this.state.demoData,
...{
'fake': {
'latitude': 52.51115736666667,
'longitude': 13.485472335185186
}
} } })
}, 5000)
}
renderMarkers () {
const vendorLocations = this.state.demoData
if (!vendorLocations) {
return null
}
return Object.keys(vendorLocations)
.map((vendorId) => {
const { latitude, longitude } = vendorLocations[vendorId]
return <Marker
onMouseUp={this.handleMouseUp(latitude, longitude)}
key={vendorId}
icon='/images/ic_locationpin.svg'
width='10'
height={10}
clickable
position={{ lat: latitude, lng: longitude }} />
})
}
}
render () {
const { userCoords } = this.props
return (
<GoogleMap
options={this.mapOptions}
defaultZoom={12}
defaultCenter={{ lat: userCoords.latitude, lng: userCoords.longitude }}
>
{this.renderMarkers()}
</GoogleMap>
)
}
}
export default withScriptjs(withGoogleMap(Map))
Steps to reproduce
- Render initial Map
- Make a state change where a new marker should show up in the map
- Updating the location of either markers will create a new one instead of updating their locations and any subsequent changes to locations of either markers will update the newly created markers.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5
Top Results From Across the Web
Reposition the center of the map when the location changes?
I can see the marker being updated, but the map stays on its defaultCenter position. import React, { Component } from 'react'; ...
Read more >My address is shown in a wrong location. Can move but not ...
Solution 1: delete marker and location "C", move marker for "A" to correct location. Solution 2: delete marker for "A" alltogether and leave ......
Read more >Using markers in the Premiere Pro timeline - Adobe Support
Get help on adding markers to clips in Premiere Pro, using markers to position and arrange clips, display marker comments, and share markers...
Read more >Work with markers in Mapbox.js | Help
You can add markers to your map using Leaflet or with GeoJSON using Mapbox.js. Add markers in Leaflet. You can add a DOM...
Read more >lightning-map - Salesforce Lightning Component Library
map-markers is an array of markers that indicate location. ... If you don't specify the zoom-level attribute, the lightning-map component calculates a zoom ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The issue seems to be due to React.StrictMode #921 , I still don’t understand why react’s strictMode would break this
@khaledosman also you can run gatsby.js example and play with it.