I'm having issues with the react-google-maps module, namely with markers and component re-rendering.
This is the scenario:
- Load page with map and 2-3 markers on it (ok)
- Click a tab on the nav-bar -> page renders something else in a single page app fashion (ok)
- Click the tab on the nav-bar that leads back to the map -> page renders the map but the markers are not rendered.
I am holding marker data in the state and i am applying them to the map in the onGoogleApiLoaded={({map, maps}) => this.renderMarkers(map, maps)}
.
I managed to solve this problem by wrapping renderMarkers
in a setTimeout
of one second and it loads fine this way.
I don't like this solution and i believe that i simply don't do it the way i should. I believe the markers try to place themselves on the map that is not rendered but it's curious that even withoutsetTimeout
it works for the default page (probably something to do with react component lifecycle, i don't know).
Is there a way to attach the property onGoogleApiLoaded={({map, maps}) => this.renderMarkers(map, maps)}
after the map is completely loaded ? Or at least delay it until i'm 100% sure that the map is loaded and not some random number of seconds.
EDIT: I was assigning the markers with the marker.setMap(map)
method and i just tried setting up the property map: map
in the marker's constructor but the behaviour stays the same.