Skip to content

Commit

Permalink
change polygon useEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
glintoo authored and jaywcjlove committed Jun 22, 2021
1 parent 0eb6639 commit e4352b2
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/Polygon/usePolygon.tsx
Original file line number Diff line number Diff line change
@@ -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 {}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e4352b2

Please sign in to comment.