Skip to content

Commit

Permalink
refactor: 改善 AMapHawkEye 实现
Browse files Browse the repository at this point in the history
  • Loading branch information
xyy94813 committed Sep 21, 2023
1 parent c10e45e commit bc58cd7
Showing 1 changed file with 9 additions and 28 deletions.
37 changes: 9 additions & 28 deletions src/components/AMapHawkEye/AMapHawkEye.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { FC } from 'react';
import {
useEffect, useState, useMemo, memo,
useEffect,
useMemo,
useCallback,
memo,
} from 'react';

import useAMap from '../../hooks/useAMap';
import useAMapPluginInstance from '../../hooks/useAMapPluginInstance';
import useVisible from '../../hooks/useVisible';
import useAMapControlBinder from '../../hooks/useAMapControlBinder';
import useAMapEventBinder from '../../hooks/useAMapEventBinder';

Expand Down Expand Up @@ -46,9 +50,6 @@ const AMapHawkEye: FC<AMapHawkEyeProps> = ({
onShow,
onHide,
}) => {
const { __AMAP__: AMap } = useAMap();
const [curInstance, setInstance] = useState<any>(null);

const initConfig = useMemo(() => {
const conf: AMap.HawkEyeOptions = {};

Expand Down Expand Up @@ -82,22 +83,8 @@ const AMapHawkEye: FC<AMapHawkEyeProps> = ({
buttonSize,
]);

useEffect(() => {
if (!AMap) {
return;
}

const initInstance = () => {
const newInstance = new AMap.HawkEye(initConfig);
setInstance(newInstance);
};

if (AMap.HawkEye) {
initInstance();
} else {
AMap.plugin('AMap.HawkEye', initInstance);
}
}, [AMap, initConfig]);
const initInstance = useCallback((AMap) => new AMap!.HawkEye(initConfig), [initConfig]);
const curInstance = useAMapPluginInstance<AMap.HawkEye>('HawkEye', initInstance);

// 是否展开
useEffect(() => {
Expand All @@ -108,13 +95,7 @@ const AMapHawkEye: FC<AMapHawkEyeProps> = ({
}
}, [curInstance, isOpen]);

useEffect(() => {
if (visible) {
curInstance?.show?.();
} else {
curInstance?.hide?.();
}
}, [curInstance, visible]);
useVisible(curInstance, !!visible);

useAMapEventBinder(curInstance, 'show', onShow);
useAMapEventBinder(curInstance, 'hide', onHide);
Expand Down

0 comments on commit bc58cd7

Please sign in to comment.