From e4352b273bb1a0ecd56d4346634c617a39d3021b Mon Sep 17 00:00:00 2001 From: glintoo Date: Tue, 22 Jun 2021 11:02:47 +0800 Subject: [PATCH] change polygon useEffect --- src/Polygon/usePolygon.tsx | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Polygon/usePolygon.tsx b/src/Polygon/usePolygon.tsx index 7666fbcf5..2d4b54aee 100644 --- a/src/Polygon/usePolygon.tsx +++ b/src/Polygon/usePolygon.tsx @@ -1,6 +1,7 @@ import { useState, useEffect, useMemo } from 'react'; import { useEnableProperties, useProperties, useVisiable, useEventProperties } from '../common/hooks'; import { PolygonProps } from './'; +import { noop } from '../utils/noop'; export interface UsePolygon extends PolygonProps {} @@ -31,14 +32,17 @@ export default function usePolygon(props = {} as UsePolygon) { enableEditing, enableClicking, }; - useMemo(() => { - if (map && !polygon) { - const points = (path || []).map((item) => new BMap.Point(item.lng, item.lat)); - const instance = new BMap.Polygon(points, opts); - map.addOverlay(instance); - setPolygon(instance); - } - }, [map, props.path, polygon]); + + useEffect(function () { + if (!BMap || !map) return noop; + const points = (path || []).map((item) => new BMap.Point(item.lng, item.lat)); + const instance = new BMap.Polygon(points, opts); + map.addOverlay(instance); + setPolygon(instance); + return function () { + map.removeOverlay(instance); + }; + }, [map, props.path]); useEffect(() => { if (path && polygon) {