Skip to content

Commit a800c89

Browse files
committed
fix: Book travel - Book travel animation becomes blank while RHP is dismissed
1 parent 821edc5 commit a800c89

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/components/Lottie/index.tsx

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {NavigationContainerRefContext, NavigationContext} from '@react-navigation/native';
22
import type {AnimationObject, LottieViewProps} from 'lottie-react-native';
33
import LottieView from 'lottie-react-native';
4-
import type {ForwardedRef} from 'react';
5-
import React, {forwardRef, useContext, useEffect, useState} from 'react';
4+
import React, {forwardRef, useContext, useEffect, useRef, useState} from 'react';
65
import {InteractionManager, View} from 'react-native';
76
import type DotLottieAnimation from '@components/LottieAnimations/types';
87
import useAppState from '@hooks/useAppState';
@@ -18,7 +17,8 @@ type Props = {
1817
shouldLoadAfterInteractions?: boolean;
1918
} & Omit<LottieViewProps, 'source'>;
2019

21-
function Lottie({source, webStyle, shouldLoadAfterInteractions, ...props}: Props, ref: ForwardedRef<LottieView>) {
20+
function Lottie({source, webStyle, shouldLoadAfterInteractions, ...props}: Props) {
21+
const animationRef = useRef<LottieView>(null);
2222
const appState = useAppState();
2323
const {splashScreenState} = useSplashScreenStateContext();
2424
const styles = useThemeStyles();
@@ -79,19 +79,20 @@ function Lottie({source, webStyle, shouldLoadAfterInteractions, ...props}: Props
7979
return unsubscribeNavigationBlur;
8080
}, [browser, navigationContainerRef, navigator]);
8181

82+
// If user is being navigated away, let pause the animation to prevent memory leak.
83+
useEffect(() => {
84+
if (!animationRef.current || !hasNavigatedAway) {
85+
return;
86+
}
87+
animationRef?.current?.pause();
88+
}, [hasNavigatedAway]);
89+
8290
// If the page navigates to another screen, the image fails to load, app is in background state, animation file isn't ready, or the splash screen isn't hidden yet,
8391
// we'll just render an empty view as the fallback to prevent
8492
// 1. memory leak, see issue: https://github.com/Expensify/App/issues/36645
8593
// 2. heavy rendering, see issues: https://github.com/Expensify/App/issues/34696 and https://github.com/Expensify/App/issues/47273
8694
// 3. lag on react navigation transitions, see issue: https://github.com/Expensify/App/issues/44812
87-
if (
88-
hasNavigatedAway ||
89-
isError ||
90-
appState.isBackground ||
91-
!animationFile ||
92-
splashScreenState !== CONST.BOOT_SPLASH_STATE.HIDDEN ||
93-
(!isInteractionComplete && shouldLoadAfterInteractions)
94-
) {
95+
if (isError || appState.isBackground || !animationFile || splashScreenState !== CONST.BOOT_SPLASH_STATE.HIDDEN || (!isInteractionComplete && shouldLoadAfterInteractions)) {
9596
return <View style={[aspectRatioStyle, props.style]} />;
9697
}
9798

@@ -100,7 +101,7 @@ function Lottie({source, webStyle, shouldLoadAfterInteractions, ...props}: Props
100101
// eslint-disable-next-line react/jsx-props-no-spreading
101102
{...props}
102103
source={animationFile}
103-
ref={ref}
104+
ref={animationRef}
104105
style={[aspectRatioStyle, props.style]}
105106
webStyle={{...aspectRatioStyle, ...webStyle}}
106107
onAnimationFailure={() => setIsError(true)}

0 commit comments

Comments
 (0)