question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Markers don't update location correctly according to state

See original GitHub issue

The 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

  1. Render initial Map
  2. Make a state change where a new marker should show up in the map
  3. 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:open
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
khaledosmancommented, Feb 1, 2019

The issue seems to be due to React.StrictMode #921 , I still don’t understand why react’s strictMode would break this

0reactions
JustFly1984commented, Feb 8, 2019

@khaledosman also you can run gatsby.js example and play with it.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found