react-leaflet icon indicating copy to clipboard operation
react-leaflet copied to clipboard

GeoJson: style changes would never be updated

Open nguyendh2601 opened this issue 10 years ago • 3 comments

render() {
    const style = _.defaults(location.style, {
      stroke: true,
      color: "#2e5696",
      opacity: 0.6,
      weight: 2,
      dashArray: "1",
      fill: true,
      fillColor: "#2e5696",
      fillOpacity: 0.3,
    });

    return (
      <GeoJson
        style={style}
        data={location.geojson}
        layerContainer={layerContainer}
        map={map}
      >
      </GeoJson>);
  }
}

i did init GeoJson as above, the style is correct. But when i change the style later, the GeoJson would not be updated. Took a look at getPathOptions in Path.js always return empty style. screen shot 2016-05-27 at 1 53 51 pm

I'm going with a workaround like this:

render() {
    return (
      <GeoJson
        key={location.id}
        style={style}
        data={location.geojson}
        onEachFeature={this.onEachFeature}
        layerContainer={layerContainer}
        map={map}
        {...style}
      >
      </GeoJson>);
}

nguyendh2601 avatar May 27 '16 07:05 nguyendh2601

L.geoJson( <Object> geojson?, <GeoJSON options> options? )

options of GeoJSONare not Path options like CircleMarker.

L.circleMarker( <LatLng> latlng, <Path options> options? )

I think we should change componentDidUpdate of _GeoJson.js_ to

componentDidUpdate(prevProps) {
    this.setStyleIfChanged(prevProps.style, this.props.style);
 }

What do you think @PaulLeCam @boromisp ? I can create PR for this

nguyendh2601 avatar May 27 '16 13:05 nguyendh2601

I believe how it currently works is the expected result because of how leafletjs works in the background, even though its not intuitive. Proposing to make the styles object a dynamic prop will break the resetStyles function.

For more information...

leafletjs will do something similar to this.options = { ...this.options, ...this.options.styles } when you initialize. This is because the options.styles is considered the source and is never changed. Instead the options object is changed when setStyles function is used. (source).

This is because leafletjs holds the initial styles for resetStyle. Setting styles={styles} will set the initial styles, so when resetStyles is called, it will use this object to resetStyles. Making the styles prop dynamic will break leafletjs's reference to the initial styles.

@PaulLeCam A bigger problem tied to GeoJson styles (for me) is that react-leaflet styles are only allowed to be objects, but leaflet allows styles to be objects or functions (source).

jgimbel avatar Jun 09 '16 16:06 jgimbel

Hi, Indeed, the style prop should be updated as expected if it is a function, that should be fixed in v0.11.6.

PaulLeCam avatar Jun 09 '16 22:06 PaulLeCam