react-google-maps: Uncaught TypeError: Cannot read property 'overlayMouseTarget' of null
I previously posted this on StackOverflow but since this is less of a “how to” question and more a case of “there might be a bug with the Overlay component”, I decided to repost it here.
When leaving the page that has the map with some OverlayView’s, I get the error message:
Uncaught TypeError: Cannot read property ‘overlayMouseTarget’ of null
Here is the code with the GoogleMap component:
const GMap = withGoogleMap((props) => (
<GoogleMap
ref={props.onMapMounted}
defaultZoom={12}
zoom={props.zoom}
onDragEnd={props.onZoomChanged}
onZoomChanged={props.onZoomChanged}
defaultCenter={props.defaultCenter}
center={props.center}
>
{
props.markers && props.markers.map((data, index) => (
<OverlayView
key={index}
position={{
lat: data.get('loc').get('geo').get('lat'),
lng: data.get('loc').get('geo').get('lng'),
}}
mapPaneName={OverlayView.OVERLAY_MOUSE_TARGET}
getPixelPositionOffset={(width, height) => ({ x: -(width / 2), y: -(height) })}
>
<WnmMarker
text={data.get('text')}
highlight={data.get('highlight')}
/>
</OverlayView>
))
}
</GoogleMap>
));
Could it be that something is missing in the componentWillUnmount?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 6
- Comments: 24 (6 by maintainers)
Commits related to this issue
- Changing location frequently This forces error "Cannot read property 'overlayMouseTarget' of undefined" (see https://github.com/tomchentw/react-google-maps/issues/482). — committed to gomezgoiri/reproducing-react-gmaps-error by gomezgoiri 7 years ago
- Wrap map markers in <a> tags, to hopefully help with SEO - Use overlays to support placing custom elements on the map. - Add custom overlay class to address https://github.com/tomchentw/react-googl... — committed to streetlives/streetlives-web by Rovack 4 years ago
Taking @foloinfo’s advice, I implemented the following class in my project that eliminates the errors. Hopefully this will help somebody else, too!
If it can help, after reading #405 , I added key={Math.random()} to
<OverlayView />and the error message disappeared.I’m not sure if this is related but the
widthandheightpassed down togetPixelPositionOffsetis sometimes wrong. I receive (0px, 13px) whereas my markers are (24px, 32px). It leads to a misplaced overlay on the map.Here is the code with the GoogleMap component
It seems like still an issue since it happens on 9.0.1. Adding key to OverlayView or wait till component to be mounted, didn’t work for me.
I could avoid by adding existence check before drawing of OverlayVIew like below.
However I’m not sure it’s a correct way to fix it. Any suggestions?
Added: In some cases I needed to make sure
this.containerElementexists as well, so modified toif(mapPanes && this.containerElement)Here’s my solution:
The error (at least for me) is pretty benign and it only shows up once when the OverlayView is first attempted to be rendered. With that in mind, I tried delaying its render until after the rest of the Google Map is mounted. So essentially, you’re allowing the map some time to load before adding the Overlay. Bonus, no need to add a key at all.
@joaoreynolds This repo is unmaintained more than a year, please refer to npm @react-google-maps/api
We have docs, examples and community.
I was getting an error when removing overlays (see below) so I also had to override
onRemovein @owap 's solution. — Super HackThe error was
Sorry, I haven’t had the time to figure it out.
We use redux with several stores that each have an
Immutable.List()of markers. When the user changes to a different section of our site we provide the map with data from the store corresponding to the given section of the site. Each Section of markers updates via callback that dispatches updates from a socket connection. Updates seem to be fine, but when we switch to a different stores markerList()is when it seems to choke.