diff --git a/packages/polyline/src/usePolyline.tsx b/packages/polyline/src/usePolyline.tsx index 60b66ec8a..edd0378e3 100644 --- a/packages/polyline/src/usePolyline.tsx +++ b/packages/polyline/src/usePolyline.tsx @@ -11,22 +11,22 @@ export function usePolyline(props = {} as UsePolyline) { const { map } = useMapContext(); useEffect(() => { if (map && !polyline) { - let instance: AMap.Polyline = new AMap.Polyline(other); + const instance: AMap.Polyline = new AMap.Polyline(other); map.add(instance); setPolyline(instance); - return () => { - if (instance) { - if (AMap.v) { - map && map.remove(instance); - } else { - // 暂不使用这个 API,这个不兼容 v1.4.xx,改用 map.remove API - map && map.removeLayer(instance); - } - setPolyline(undefined); - } - }; } - }, [map]); + return () => { + if (polyline) { + if (AMap.v) { + map && map.remove(polyline); + } else { + // 暂不使用这个 API,这个不兼容 v1.4.xx,改用 map.remove API + map && map.removeLayer(polyline); + } + setPolyline(undefined); + } + }; + }, [map, polyline]); useEffect(() => { if (polyline) {