diff --git a/src/components/MapView/MapView.web.tsx b/src/components/MapView/MapView.web.tsx index d706168f076f..8442202b5ca8 100644 --- a/src/components/MapView/MapView.web.tsx +++ b/src/components/MapView/MapView.web.tsx @@ -19,9 +19,10 @@ import utils from './utils'; const MapView = forwardRef( ({style, styleURL, waypoints, mapPadding, accessToken, directionCoordinates, initialState = {location: CONST.MAPBOX.DEFAULT_COORDINATE, zoom: CONST.MAPBOX.DEFAULT_ZOOM}}, ref) => { const [mapRef, setMapRef] = useState(null); + const [shouldResetBoundaries, setShouldResetBoundaries] = useState(false); const setRef = useCallback((newRef: MapRef | null) => setMapRef(newRef), []); - useEffect(() => { + const resetBoundaries = useCallback(() => { if (!waypoints || waypoints.length === 0) { return; } @@ -47,6 +48,19 @@ const MapView = forwardRef( map.fitBounds([northEast, southWest], {padding: mapPadding}); }, [waypoints, mapRef, mapPadding, directionCoordinates]); + // Reset boundaries when waypoints change + useEffect(resetBoundaries, [resetBoundaries]); + + useEffect(() => { + if (!shouldResetBoundaries) { + return; + } + + resetBoundaries(); + setShouldResetBoundaries(false); + // eslint-disable-next-line react-hooks/exhaustive-deps -- this effect only needs to run when the boundaries reset is forced + }, [shouldResetBoundaries]); + useEffect(() => { if (!mapRef) { return; @@ -54,6 +68,7 @@ const MapView = forwardRef( const resizeObserver = new ResizeObserver(() => { mapRef.resize(); + setShouldResetBoundaries(true); }); resizeObserver.observe(mapRef.getContainer());