简体   繁体   English

使用方法 setPaths 的问题(Leaflet PolylineDecorator)

[英]Problem with using method setPaths ( Leaflet PolylineDecorator)

I wanna update the component and change the lines.我想更新组件并更改行。

When I try to send new props with coordinates, old coordinates also staying.当我尝试发送带有坐标的新道具时,旧坐标也会保留。

const PolylineDecorator = withLeaflet(props => {

const { positions } = props;
const polyRef = useRef();

useEffect(() => {
const polyline = polyRef.current.leafletElement; //get native Leaflet polyline
const { map } = polyRef.current.props.leaflet; //get native Leaflet map

L.polylineDecorator(polyline, {
 patterns: props.patterns,
}).addTo(map);
}, []);
return <Polyline ref={polyRef} {...props} opacity="0" />;
});

在此处输入图片说明

It is tricky to debug this one out without trying in the local machine.不尝试在本地机器上调试这个是很棘手的。 But It looks like you need to pass positions to the useEffect as follows, so it will re-render every time positions are changing.但是看起来您需要按如下方式将positions传递给useEffect ,因此每次位置更改时它都会重新渲染。

useEffect(() => {
 const polyline = polyRef.current.leafletElement; //get native Leaflet 
 polyline
 const { map } = polyRef.current.props.leaflet; //get native Leaflet map

 L.polylineDecorator(polyline, {
 patterns: props.patterns,
 }).addTo(map);
}, [positions]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM