react-bmapgl icon indicating copy to clipboard operation
react-bmapgl copied to clipboard

CustomOverlay 会受因为其它 state 修改后发生的 rerender 影响,导致闪动

Open lcysgsg opened this issue 4 years ago • 5 comments

文字看不出来,图片很明显,一闪一闪的

代码

 const [test, setTest] = React.useState(0);

  React.useEffect(() => {
    setInterval(() => {
      setTest((state) => state + 1);
    }, 500);
  }, []);


  <Map
    ref={mapCallback}
    center={mapCenter}
    zoom={mapZoom.current}
    maxZoom={mapZoom.max}
    minZoom={mapZoom.min}
    enableScrollWheelZoom={true}
    style={{ position: 'fixed', left: 0, top: 0, width: '100%', height: '100%' }}
  >
      <CustomOverlay position={new BMapGL.Point(120.0538590588235, 27.611044541630818)}>
        <img src={require('@/assets/maintain-car.png')} width="63" />
      </CustomOverlay>
   </Map>

lcysgsg avatar Jun 07 '21 02:06 lcysgsg

闪烁可能是每次update都加载图片导致,图片预先import吧

Junior2Ran avatar Jun 07 '21 02:06 Junior2Ran

@Junior2Ran 不行,改成 import 还是一样的问题

import imgMaintainCar from '@/assets/maintain-car.png';

<CustomOverlay
  map={mapRef.current}
  position={new BMapGL.Point(120.0538590588235, 27.611044541630818)}
>
  <img src={imgMaintainCar} />
</CustomOverlay>

lcysgsg avatar Jun 07 '21 02:06 lcysgsg

是因为 CustomOverlay被销毁又重新创建的问题,每次只要state或是props发生改变 都会

hblz avatar Dec 22 '21 09:12 hblz

感觉是React cloneElement 引起的

hblz avatar Dec 22 '21 09:12 hblz

隔壁的 @uiw/react-baidu-map 基于 createPortal 支持得很好,不过只支持 BMap。可以参照他们的源码重写一遍,并把 BMap 换成 BMapGL

https://github.com/uiwjs/react-baidu-map/blob/master/packages/custom-overlay/src/index.tsx

lyh543 avatar Apr 26 '23 01:04 lyh543