Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display GeoJson properties without using useState #20

Open
gepp4 opened this issue Feb 1, 2024 · 1 comment
Open

Display GeoJson properties without using useState #20

gepp4 opened this issue Feb 1, 2024 · 1 comment

Comments

@gepp4
Copy link

gepp4 commented Feb 1, 2024

Hi. I have a custom Control element in my react-leaflet map that looks like this:

<Control prepend position="topright">
  {hoveredShape && (
    <div className="...">
      {hoveredShape}
    </div>
  )}
</Control>;

I use it to display the name of the geojson shape I'm hovering (a city, a region, a state).

I set hoveredShape inside the mouseover event listener:

layer.on({
  mouseover: (e: any) => {
    let layer = e.target;
    layer.setStyle({
      weight: 2,
      color: "#666",
      dashArray: "",
      fillOpacity: 0.7,
    });

    layer.bringToFront();

    if (feature.properties.hasOwnProperty("name")) {
      setHoveredShape(feature.properties.name);
    } else if (feature.properties.hasOwnProperty("prov_name")) {
      setHoveredShape(feature.properties.prov_name);
    } else {
      setHoveredShape(feature.properties.reg_name);
    }
  },

 ...

Unfortunately this way makes the component re-render every time I hover on a diffrent shape. Is there a way I can access the shape's properties in my Control element without using React's state?

Thanks.

@AntoninoGargiuloBID
Copy link

news on this? any workaround?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants