From a5bf00dfae07701e3a5c8f4e2f74ade742d06d60 Mon Sep 17 00:00:00 2001 From: hurali97 Date: Tue, 20 Aug 2024 13:26:56 +0500 Subject: [PATCH] fix: cannot read property routes of undefined --- .../setupCustomAndroidBackHandler/index.android.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/libs/Navigation/setupCustomAndroidBackHandler/index.android.ts b/src/libs/Navigation/setupCustomAndroidBackHandler/index.android.ts index 10aa8b99a484..107dae2bb74c 100644 --- a/src/libs/Navigation/setupCustomAndroidBackHandler/index.android.ts +++ b/src/libs/Navigation/setupCustomAndroidBackHandler/index.android.ts @@ -13,8 +13,7 @@ type SearchPageProps = StackScreenProps { const rootState = navigationRef.getRootState(); - - const bottomTabRoute = rootState.routes.find((route) => route.name === NAVIGATORS.BOTTOM_TAB_NAVIGATOR); + const bottomTabRoute = rootState?.routes?.find((route) => route.name === NAVIGATORS.BOTTOM_TAB_NAVIGATOR); const bottomTabRoutes = bottomTabRoute?.state?.routes; const focusedRoute = findFocusedRoute(rootState); @@ -23,7 +22,7 @@ function setupCustomAndroidBackHandler() { return false; } - const isLastScreenOnStack = bottomTabRoutes.length === 1 && rootState.routes.length === 1; + const isLastScreenOnStack = bottomTabRoutes.length === 1 && rootState?.routes?.length === 1; if (NativeModules.HybridAppModule && isLastScreenOnStack) { NativeModules.HybridAppModule.exitApp(); @@ -35,7 +34,7 @@ function setupCustomAndroidBackHandler() { navigationRef.dispatch({...StackActions.pop(), target: bottomTabRoute?.state?.key}); navigationRef.dispatch({...StackActions.pop()}); - const centralPaneRouteAfterPop = getTopmostCentralPaneRoute({routes: [rootState.routes.at(-2)]} as State); + const centralPaneRouteAfterPop = getTopmostCentralPaneRoute({routes: [rootState?.routes?.at(-2)]} as State); const bottomTabRouteAfterPop = bottomTabRoutes.at(-2); // It's possible that central pane search is desynchronized with the bottom tab search. @@ -57,7 +56,7 @@ function setupCustomAndroidBackHandler() { // It's possible that central pane search is desynchronized with the bottom tab search. // e.g. opening a tab different than search will wipe out central pane screens. // In that case we have to push the proper one. - if (bottomTabRoutes && bottomTabRoutes?.length >= 2 && bottomTabRoutes[bottomTabRoutes.length - 2].name === SCREENS.SEARCH.BOTTOM_TAB && rootState.routes.length === 1) { + if (bottomTabRoutes && bottomTabRoutes?.length >= 2 && bottomTabRoutes[bottomTabRoutes.length - 2].name === SCREENS.SEARCH.BOTTOM_TAB && rootState?.routes?.length === 1) { const {policyID, ...restParams} = bottomTabRoutes[bottomTabRoutes.length - 2].params as SearchPageProps['route']['params']; navigationRef.dispatch({...StackActions.push(SCREENS.SEARCH.CENTRAL_PANE, {...restParams, policyIDs: policyID})}); navigationRef.dispatch({...StackActions.pop(), target: bottomTabRoute?.state?.key});