Skip to content

Commit

Permalink
Merge pull request #29270 from ahmedGaber93/issue-28337
Browse files Browse the repository at this point in the history
fix MapView.tsx cleanup when blur
  • Loading branch information
neil-marcellini authored Oct 12, 2023
2 parents 10ff92c + 6dba61c commit 1bd4c97
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/components/MapView/MapView.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {View} from 'react-native';
import {useFocusEffect} from '@react-navigation/native';
import {useFocusEffect, useNavigation} from '@react-navigation/native';
import Mapbox, {MapState, MarkerView, setAccessToken} from '@rnmapbox/maps';
import {forwardRef, memo, useCallback, useEffect, useImperativeHandle, useRef, useState} from 'react';
import styles from '../../styles/styles';
Expand All @@ -14,6 +14,7 @@ import {MapViewProps, MapViewHandle} from './MapViewTypes';
const MapView = forwardRef<MapViewHandle, MapViewProps>(({accessToken, style, mapPadding, styleURL, pitchEnabled, initialState, waypoints, directionCoordinates, onMapReady}, ref) => {
const cameraRef = useRef<Mapbox.Camera>(null);
const [isIdle, setIsIdle] = useState(false);
const navigation = useNavigation();

useImperativeHandle(
ref,
Expand All @@ -30,6 +31,7 @@ const MapView = forwardRef<MapViewHandle, MapViewProps>(({accessToken, style, ma
// When the page regains focus, the onIdled method of the map will set the actual "idled" state,
// which in turn triggers the callback.
useFocusEffect(
// eslint-disable-next-line rulesdir/prefer-early-return
useCallback(() => {
if (waypoints?.length && isIdle) {
if (waypoints.length === 1) {
Expand All @@ -46,12 +48,16 @@ const MapView = forwardRef<MapViewHandle, MapViewProps>(({accessToken, style, ma
cameraRef.current?.fitBounds(northEast, southWest, mapPadding, 1000);
}
}
return () => {
setIsIdle(false);
};
}, [mapPadding, waypoints, isIdle, directionCoordinates]),
);

useEffect(() => {
const unsubscribe = navigation.addListener('blur', () => {
setIsIdle(false);
});
return unsubscribe;
}, [navigation]);

useEffect(() => {
setAccessToken(accessToken);
}, [accessToken]);
Expand Down

0 comments on commit 1bd4c97

Please sign in to comment.