diff --git a/src/libs/Navigation/AppNavigator/createRootStackNavigator/useCustomRootStackNavigatorState/index.ts b/src/libs/Navigation/AppNavigator/createRootStackNavigator/useCustomRootStackNavigatorState/index.ts index 88a7135aa0b3..4f09f87a22e0 100644 --- a/src/libs/Navigation/AppNavigator/createRootStackNavigator/useCustomRootStackNavigatorState/index.ts +++ b/src/libs/Navigation/AppNavigator/createRootStackNavigator/useCustomRootStackNavigatorState/index.ts @@ -2,17 +2,11 @@ import {isFullScreenName} from '@libs/Navigation/helpers/isNavigatorName'; import type {CustomStateHookProps} from '@libs/Navigation/PlatformStackNavigation/types'; // This is an optimization to keep mounted only last few screens in the stack. +// We keep the last full screen and the one before it to avoid unmounting persistent screens +// (like ReportsSplitNavigator) which contain heavy component trees (e.g. LHN with thousands of items). export default function useCustomRootStackNavigatorState({state}: CustomStateHookProps) { const lastSplitIndex = state.routes.findLastIndex((route) => isFullScreenName(route.name)); - let indexToSlice = Math.max(0, lastSplitIndex); - const hasPrevRoute = lastSplitIndex > 0; - const isPrevFullScreen = isFullScreenName(state.routes.at(lastSplitIndex - 1)?.name); - - // If the route before the last full screen is e.g. RHP, we should leave it in the rendered routes, - // as there may be display issues (blank screen) when navigating back and recreating that route to render. - if (hasPrevRoute && !isPrevFullScreen) { - indexToSlice = lastSplitIndex - 1; - } + const indexToSlice = Math.max(0, lastSplitIndex - 1); const routesToRender = state.routes.slice(indexToSlice, state.routes.length); return {...state, routes: routesToRender, index: routesToRender.length - 1}; } diff --git a/src/libs/Navigation/AppNavigator/usePreloadFullScreenNavigators.ts b/src/libs/Navigation/AppNavigator/usePreloadFullScreenNavigators.ts index 8c1adcde02e7..ca424767b0ca 100644 --- a/src/libs/Navigation/AppNavigator/usePreloadFullScreenNavigators.ts +++ b/src/libs/Navigation/AppNavigator/usePreloadFullScreenNavigators.ts @@ -134,6 +134,7 @@ function usePreloadFullScreenNavigators() { } hasPreloadedRef.current = true; setTimeout(() => { + const currentRoutes = navigation.getState().routes; for (const tabName of TABS_TO_PRELOAD) { // Don't preload the current tab const isCurrentTab = TAB_TO_FULLSCREEN[tabName].includes(route.name as FullScreenName); @@ -147,6 +148,13 @@ function usePreloadFullScreenNavigators() { continue; } + // Don't preload tabs whose navigator is already in the regular routes stack + const isRouteInStack = currentRoutes.some((r) => TAB_TO_FULLSCREEN[tabName].includes(r.name as FullScreenName)); + + if (isRouteInStack) { + continue; + } + // Preload everything else preloadTab(tabName, navigation, subscriptionPlan); }