mhasbie / react-leaflet-deflate

React wrapper of Leaflet.Deflate. Substitues polygons and lines with markers when screen size falls below a defined threshold.
MIT License
6 stars 5 forks source link

Cannot read property 'layerContainer' of undefined #2

Closed bslipek closed 5 years ago

bslipek commented 5 years ago
"leaflet": "^1.3.4",
"react-leaflet": "^1.9.1",
"react-leaflet-deflate": "^2.1.0",

I get Cannot read property 'layerContainer' of undefined.

react_app

I belive here is the problem. Shouldn'tlayerContainer come from context?

Sobak commented 5 years ago

:+1:

mhasbie commented 5 years ago

Hi,

Can you post your codes here or a jsfiddle or codepen to replicate this?

Thanks

bslipek commented 5 years ago

Here is it!

Edit 5vr1yyyxkx

Can you make this async example works? (Just wait 5sec for crash)

daszkowski commented 5 years ago

+1

slaby93 commented 5 years ago

+1

bslipek commented 5 years ago

Here is some progress:

  1. I can add features dynamically
  2. There should be markers but can;t display it in codesanbox, works locally ;/

Edit 5yq6pv2nvp

But: 1. still can't make markerCluster={true} 2. when you place space somewhere in the editor error appear, probably related to rerender component.

bslipek commented 5 years ago

Here is fixed version with new way of remove layers, not perfect but works:

removeLayers() {
    let layers = this.layerContainer._layers;
    let layer_ids = Object.keys(layers);

    layer_ids.forEach(id => {
      let layer = layers[id];
      if (layer._url) return; // <- prevent removing tile layer, only features group, not nice ;/
      this.layerContainer.removeLayer(layer);
    });
  }

Edit React Leaflet Deflate Fixed

We still can't use markerCluster. To do this we need to modify Leaflet library:

// leaflet-src.js line 70
function stamp(obj) {
  if (!obj) obj = {}; // <- here
  obj._leaflet_id = obj._leaflet_id || ++lastId;

  return obj._leaflet_id;
}
bslipek commented 5 years ago

I created PR for Leaflet lib. https://github.com/Leaflet/Leaflet/pull/6381

mhasbie commented 5 years ago

This issue is now fixed in v2.2.0. Thanks for reporting.

bslipek commented 5 years ago

It is much better now! One thing I want to suggest:

// Deflate.js line 53
if (toProps.data && toProps.data.features && toProps.data.features.length > 0) 
   Leaflet.geoJSON(toProps.data, toProps).addTo(this.leafletElement);

because when we try add empty FeatureCollection then Leaflet.Deflate thow error.