【发布时间】:2022-02-18 18:30:49
【问题描述】:
在过去的几天里,这一直给我带来问题。考虑一下这个简单的代码,在您按下按钮时更改圆圈的颜色。
const handleClick = (e) => {
if (color === "red") {
setColor("blue");
} else {
setColor("red");
}
};
<button style={{ textAlign: "center" }} onClick={handleClick}>
Click me to change colour!
</button>
<MapContainer center={position} zoom={13} style={{ height: "100vh" }}>
<TileLayer
attribution='&copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
{lightRailStop.features.map((feature, index) => {
return (
<Circle
center={[
feature.geometry.coordinates[1],
feature.geometry.coordinates[0]
]}
fillColor={color} //this part is set by a function
radius={200}
weight={1}
opacity={1}
fillOpacity={0.8}
/>
);
})}
</MapContainer>
当您使用react-leaflet v2 时,它可以工作。但是,v3 完全打破了这一点,我不知道为什么。
非常感谢您在这方面的帮助,因为我差点把头发扯下来试图让简单的事情发挥作用
codebox(可以使用依赖框来改变版本) https://codesandbox.io/s/react-leaflet-geojson-oneachfeature-popup-with-custom-react-component-forked-mx6pd8?file=/src/App.js
注意:MapContainer 不适用于 v2 -> 需要替换为 Map
【问题讨论】:
标签: reactjs leaflet react-leaflet