Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 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 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...