Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

Changing fill values in react-leaflet during runtime

Part of my program is supposed to change the color of countries on a react-leaflet map when that country name is entered into a text field. I am using GeoJSON data in the following way:

<MapContainer  style={{height: "80vh"}} zoom={2} center={[20, 100]}> 
    <GeoJSON data={mapData.features} style={this.countryStyle} ></GeoJSON>
</MapContainer>

Style is then defined as:

countryStyle(feature) {
    
    if(!(this.state.selectedCountries.get(feature.properties.ISO_A3) === undefined)) {
      console.log("We have found the candidate.")
      return {
        fillColor: "green",
        fillOpacity: 1
      };
    }

    return {
      fillColor: "black",
      fillOpacity: 0,
      color: "black",
      weight: .5
    };
  }

It returns that it has found the candidate, yet it refuses to make the changes to the style. Because the conditional is not true when the program starts, it keeps the original styling.

I originally tried putting this same conditional on the onEachFeature method, but my understanding is that that only runs once, while the style updates each render. I also tried making the original styling completely transparent in case it was just trying to override, but that didn't work either. I'm pretty new to this, so if this is entirely wrong, just let me know. Thanks!

question from:https://stackoverflow.com/questions/65835251/changing-fill-values-in-react-leaflet-during-runtime

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...