Open timofei-iatsenko opened 11 months ago
I can see why it's happening, but I don't have a good Idea how to fix it properly.
useMapInstance()
hook and stored in a state variable: https://github.com/visgl/react-google-maps/blob/9422f03d18cafd98ae161706e3ca797a35407db2/src/components/map/index.tsx#L185-L217useMapEvents()
hook where event-handlers are attached in another effect-hook: https://github.com/visgl/react-google-maps/blob/9422f03d18cafd98ae161706e3ca797a35407db2/src/components/map/index.tsx#L86-L90So there could be a very short gap between creating the map instance / the setMap
call and adding, since I think the setMap will cause a re-render and the updated map-value will only be available in the next render cycle, which is probably enough time for the maps api to initialize and trigger the initial events.
Description
Due to how this library binds event handlers to the google maps object there is a race condition. Events might be fired before react event handler is attached to the map. The simplest example is
onProjectionChanged
:Steps to Reproduce
Try to reload map few times, and
onProjectionChanged
called not every time.This happened due to map instance created in one
useEffect
and events bound in another. They are executed in diffrent async cycles, so map sometimes managed to fire an event before handler attached.PS I tried to create a codesandbox with an issue, but in the sandbox no events were fired at all https://codesandbox.io/p/devbox/amazing-bohr-v65hqw
Environment
Logs
No response