Skip to content

Crash when unmounting leaflet map in React: "TypeError: Cannot read properties of undefined (reading '_zoom')" #1116

@tmadeira

Description

@tmadeira

This package is some times crashing when used inside React:

TypeError: Cannot read properties of undefined (reading '_zoom')
    at i.addLayer (MarkerClusterGroup.js:127:33)
    at i._moveChild (MarkerClusterGroup.js:721:8)
    at i._childMarkerMoved (MarkerClusterGroup.js:708:9)
    ...

(I used "some times" because it doesn't always happen; it might depend on a race condition)

The crash seems to be preventable with:

diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js
index 87c992e..c75b306 100644
--- a/src/MarkerClusterGroup.js
+++ b/src/MarkerClusterGroup.js
@@ -123,10 +123,8 @@ export var MarkerClusterGroup = L.MarkerClusterGroup = L.FeatureGroup.extend({
                //Work out what is visible
                var visibleLayer = layer,
                    currentZoom = this._zoom;
-               if (layer.__parent) {
-                       while (visibleLayer.__parent._zoom >= currentZoom) {
-                               visibleLayer = visibleLayer.__parent;
-                       }
+               while (visibleLayer.__parent && visibleLayer.__parent._zoom >= currentZoom) {
+                       visibleLayer = visibleLayer.__parent;
                }

                if (this._currentShownBounds.contains(visibleLayer.getLatLng())) {

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions