0

我正在使用反应简单的地图。我创建了一张美国地图,我需要在点击时更改州的颜色。我可以通过向 Geography 组件添加新闻样式道具来更改它,但是当单击事件结束时颜色会恢复为默认颜色。有没有办法让点击的颜色保持不变?

4

1 回答 1

0

您必须存储 clickedCity 并经过这样的检查

  const [clickedCity, setClickedCity] = useState("");
  const handleClick = (geo) => {
    setClickedCity(geo.properties.name);
    dispatcher(getState({ value: geo.properties.name }));
    history.push({
      pathname: "/issues",
      state: { states: geo.properties.name },
    });
  };
           
 <Geographies geography={TUNISIA_TOPO_JSON}>
          {({ geographies }) =>
            geographies.map((geo) => {
              const isClicked = clickedCity === geo.properties.name;
              return (
                <Geography
                  key={geo.rsmKey}
                  geography={geo}
                  fill={isClicked ? "blue" : "red"}
                  stroke="#88c399"
                  strokeWidth="4"
                  onMouseEnter={onMouseEnter(geo, current)}
                  onMouseLeave={onMouseLeave}
                  onClick={() => handleClick(geo)}
                  style={{...}}
                />
              );
            })
          }
</Geographies>
于 2021-08-11T00:07:57.680 回答