uiwjs / react-amap

基于 React 封装的高德地图组件,帮助你轻松的接入地图到 React 项目中。
https://uiwjs.github.io/react-amap
MIT License
422 stars 69 forks source link

每次render都会更新到最初地图的中心点 #217

Open CodeMonkeyLin opened 2 years ago

CodeMonkeyLin commented 2 years ago

https://github.com/uiwjs/react-amap/blob/c04525cb251c763b9711b1ce72cfdb94f042d855/packages/map/src/useMap.tsx#L68

CodeMonkeyLin commented 2 years ago

这个效果应该不对,比如我移动了地图,然后操作了当前页面的筛选,结果地图回到了最初的位置

jaywcjlove commented 2 years ago

@CodeMonkeyLin 请提供示例,我这里测试 没有问题的样子

CodeMonkeyLin commented 2 years ago

@jaywcjlove 先移动地图,然后点击按钮,就发现地图回到原来位置了

import React, { useEffect, useRef, useState } from 'react';
import ReactDOM from 'react-dom';
import {
  APILoader,
  Provider,
  useMapContext,
  useMap,
  Map,
  Marker,
} from '@uiw/react-amap';
const MapDemo = () => {
  const warpper = useRef(null);
  const { map, state } = useMapContext();
  const [count, setCount] = useState(0);
  const { setContainer } = useMap({
    container: warpper.current,
    center: [104.937478, 35.439575],
    zoom: 5,
  });

  useEffect(() => {
    if (warpper.current) {
      setContainer(warpper.current);
    }
  }, [warpper.current]);

  return (
    <div style={{ height: 800 }}>
      <div ref={warpper}>
        <Marker
          position={[104.937478, 35.439575]}
          content='<div style="background-color: hsla(180, 100%, 50%, 0.3); height: 18px; width: 18px; border: 1px solid hsl(180, 100%, 40%); border-radius: 12px; box-shadow: hsl(180, 100%, 50%) 0px 0px 3px;"></div>'
        ></Marker>
      </div>
      <button
        onClick={() => {
          setCount(count + 1);
        }}
      >
        {count}
      </button>
    </div>
  );
};

export default function App() {
  return (
    <div style={{ height: 800 }}>
      <APILoader
        plugin={['AMap.MoveAnimation']}
        akay="a7a90e05a37d3f6bf76d4a9032fc9129"
      >
        <Provider>
          <MapDemo />
        </Provider>
      </APILoader>
    </div>
  );
}
jaywcjlove commented 2 years ago

@CodeMonkeyLin 你点击重新渲染了地图,你的地图渲染可以放到组件 MapDemo 外面