【发布时间】:2020-04-09 09:41:52
【问题描述】:
我有以下代码 - 想法是创建一条动画折线(用于地图)。我在互联网上找到了一些东西,但它是使用“旧”方法/没有useEffect、useState...
我无法清除else 中的polylinePath 数组 - 我试图打印出当前长度,但它总是从props.Direction 返回初始状态长度。你能帮助我吗?我不知道为什么我的 AnimatedPolyline 不起作用。
import React, { useState, useEffect, Fragment } from 'react';
import { Polyline } from 'react-native-maps';
export default function AnimatedPolyline(props) {
const [polylinePath, setPolylinePath] = useState(props.Direction);
useEffect(() => {
const interval = setInterval(() => {
animatePolylineStart();
}, 70);
return () => clearInterval(interval);
}, [props.Direction]); //tried [], too
const animatePolylineStart = () => {
if (polylinePath.length < props.Direction.length) {
const Direction = props.Direction;
const polylinePathTemp = [
...Direction.slice(0, polylinePath.length - 1)
];
setPolylinePath(polylinePathTemp);
} else {
setPolylinePath([]);
}
};
return (
<Fragment>
{
(polylinePath.length > 0) && <Polyline
coordinates={polylinePath}
strokeColor="#484848"
strokeWidth={5}
/>
}
</Fragment>
);
}
【问题讨论】:
标签: javascript reactjs react-native use-state