Reactjs 为什么不使用Mapbox删除侦听器

Reactjs 为什么不使用Mapbox删除侦听器,reactjs,mapbox,mapbox-gl-js,Reactjs,Mapbox,Mapbox Gl Js,我正在使用react map gl,并尝试将边界调整到层的bbox 我使用moveend操作保存完成时的纬度和经度,并取消删除侦听器,但它不起作用,我不知道为什么 如何解决呢 const updateViewportOnFinishFly = (mapGL: any) => { if (!mapGL) return; mapGL.off("moveend"); const centerMap = center(leaksLocationMapSt

我正在使用react map gl,并尝试将边界调整到层的bbox

我使用moveend操作保存完成时的纬度和经度,并取消删除侦听器,但它不起作用,我不知道为什么

如何解决呢

const updateViewportOnFinishFly = (mapGL: any) => {
    if (!mapGL) return;
    mapGL.off("moveend");
    const centerMap = center(leaksLocationMapState.topologyData);
    const latitude: number = centerMap?.geometry?.coordinates[1];
    const longitude: number = centerMap?.geometry?.coordinates[0];
    const zoom: number = mapGL.getZoom();
    if (isNaN(latitude) || isNaN(longitude) || isNaN(zoom)) return;
    setLeaksLocationMapState({
        ...leaksLocationMapState,
        viewport: {
            width: window.screen.width,
            height: height,
            latitude: latitude,
            longitude: longitude,
            zoom: zoom,
        },
    });
};

useEffect(() => {
    if (leaksLocationMapState.topologyData?.features?.length > 0) {
        const boundingBox = bbox(leaksLocationMapState.topologyData);
        const mapGL = mapRef?.current;
        if (mapGL !== null) {
            mapGL.getMap().fitBounds(boundingBox, { padding: 40 });
            mapGL.getMap().on("moveend", () => {
                updateViewportOnFinishFly(mapGL.getMap());
            });
        }
    }
}, []);