React Leaflet:动态设置多边形的样式

问题描述 投票:0回答:1

如何以编程方式更改多边形的颜色?

我用于GeoJSONs here的解决方案不起作用。虽然当我检查元素时,我可以看到

style:{color: "red"}

尽管如此,地图显示的是蓝色多边形。

这是我的组件的相关部分:

render() {
    const {
        id,
        name,
        geoJSON,
        zoomLevel,
        selectedPlot,
        plotBeingEdited
    } = this.props;
    const markerPosition = position(geoJSON);

    let style = () => {
        return {
            color: 'blue'
        };
    };
    if (selectedPlot === id) {
        style = () => {
            return {
                color: 'red'
            };
        };
    }

    if (zoomLevel > 14 && plotBeingEdited === id) {
        return <PlotPolygon id={id} geoJSON={geoJSON} />;
    } else if (zoomLevel > 14) {
        return (
            <Polygon
                ref="plot"
                style={style()}
                id={id}
                positions={[positions(this.props)]}
                onClick={() => {
                    this.props.selectPlot(id);
                }}
            />
        );
    }
reactjs leaflet react-leaflet
1个回答
1
投票

将颜色道具作为对象传递:

<Polygon
   ...
   color={selectedPlot === id ? 'red' : 'blue'}
/>
© www.soinside.com 2019 - 2024. All rights reserved.