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.

Uncaught TypeError: Cannot read property 'overlayMouseTarget' of null

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:24 (6 by maintainers)

github_iconTop GitHub Comments

21reactions
owapcommented, Nov 29, 2017

Taking @foloinfo’s advice, I implemented the following class in my project that eliminates the errors. Hopefully this will help somebody else, too!

import React from 'react';
import ReactDOM from 'react-dom';
import invariant from 'invariant';
import { OverlayView } from 'react-google-maps';
import { OVERLAY_VIEW } from 'react-google-maps/lib/constants';

export default class CustomOverlayView extends OverlayView {
  // Override draw function to catch errors with map panes being undefined to prevent console errors
  draw() {
    const { mapPaneName } = this.props;
    invariant(
      !!mapPaneName,
      `OverlayView requires either props.mapPaneName or props.defaultMapPaneName but got %s`,
      mapPaneName
    );
    // https://developers.google.com/maps/documentation/javascript/3.exp/reference#MapPanes
    const mapPanes = this.state[OVERLAY_VIEW].getPanes();
    if (mapPanes && this.containerElement) { // <-- Add conditional to ensure panes and container exist before drawing
      mapPanes[mapPaneName].appendChild(this.containerElement);

      ReactDOM.unstable_renderSubtreeIntoContainer(
        this,
        React.Children.only(this.props.children),
        this.containerElement,
        this.onPositionElement
      );
    }
  }
}
8reactions
amangeotcommented, May 11, 2017

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 width and height passed down to getPixelPositionOffset is 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

      <GoogleMap ref={this.handleMapRef} {...this.optionalProps}
        defaultCenter={defaultCenter} defaultOptions={defaultOptions} defaultZoom={defaultZoom}
        onDragStart={this.handleDragStart} onIdle={this.handleIdle}>
        {this._selectedMarker && (
          <OverlayView /***** --> key={Math.random()} <-- *****/ position={{ lat, lng }}
            getPixelPositionOffset={this.getPixelPositionOffset}
            defaultMapPaneName={OverlayView.OVERLAY_MOUSE_TARGET}>
            <img src={getMarkerIcon(poiType, true)} />
          </OverlayView>
        )}
        <MarkerClusterer onMarkerClick={this.handleMarkerClick} />
      </GoogleMap>
Read more comments on GitHub >

github_iconTop Results From Across the Web

react-google-maps Uncaught TypeError: Cannot read property ...
It seems that when you leave the page, the map element no longer exists but the event bound to it is still there!...
Read more >
Cannot read property 'overlayMouseTarget' of null-Googlemaps
Coding example for the question react-google-maps Uncaught TypeError: Cannot read property 'overlayMouseTarget' of null-Googlemaps.
Read more >
Cannot read property 'overlayMouseTarget' of null
Uncaught TypeError : Cannot read property 'overlayMouseTarget' of null.
Read more >
TypeError: Cannot read property 'map' of null in React.js
log(users); return ( <div> {/* ⛔️ users is `null` because API has not responded yet */} {/* ⛔️ Uncaught TypeError: Cannot read properties...
Read more >
Cannot read properties of null (reading 'contains')error in react ...
TypeError : Cannot read properties of null (reading 'contains'), Need useEffect Cleanup,EventBubblingtypeerror cannot read property of null ...
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