diff --git a/src/libs/Navigation/Navigation.js b/src/libs/Navigation/Navigation.js index 2629d36999bf..bfc0f509373e 100644 --- a/src/libs/Navigation/Navigation.js +++ b/src/libs/Navigation/Navigation.js @@ -98,6 +98,40 @@ function getDistanceFromPathInRootNavigator(path) { return -1; } +/** + * Returns the current active route + * @returns {String} + */ +function getActiveRoute() { + const currentRoute = navigationRef.current && navigationRef.current.getCurrentRoute(); + const currentRouteHasName = lodashGet(currentRoute, 'name', false); + if (!currentRouteHasName) { + return ''; + } + + const routeFromState = getPathFromState(navigationRef.getRootState(), linkingConfig.config); + + if (routeFromState) { + return routeFromState; + } + + return ''; +} + +/** + * Check whether the passed route is currently Active or not. + * + * Building path with getPathFromState since navigationRef.current.getCurrentRoute().path + * is undefined in the first navigation. + * + * @param {String} routePath Path to check + * @return {Boolean} is active + */ +function isActiveRoute(routePath) { + // We remove First forward slash from the URL before matching + return getActiveRoute().substring(1) === routePath; +} + /** * Main navigation method for redirecting to a route. * @param {String} route @@ -111,7 +145,7 @@ function navigate(route = ROUTES.HOME, type) { pendingRoute = route; return; } - linkTo(navigationRef.current, route, type); + linkTo(navigationRef.current, route, type, isActiveRoute(route)); } /** @@ -221,26 +255,6 @@ function dismissModal(targetReportID) { } } -/** - * Returns the current active route - * @returns {String} - */ -function getActiveRoute() { - const currentRoute = navigationRef.current && navigationRef.current.getCurrentRoute(); - const currentRouteHasName = lodashGet(currentRoute, 'name', false); - if (!currentRouteHasName) { - return ''; - } - - const routeFromState = getPathFromState(navigationRef.getRootState(), linkingConfig.config); - - if (routeFromState) { - return routeFromState; - } - - return ''; -} - /** * Returns the current active route without the URL params * @returns {String} @@ -265,20 +279,6 @@ function getRouteNameFromStateEvent(event) { } } -/** - * Check whether the passed route is currently Active or not. - * - * Building path with getPathFromState since navigationRef.current.getCurrentRoute().path - * is undefined in the first navigation. - * - * @param {String} routePath Path to check - * @return {Boolean} is active - */ -function isActiveRoute(routePath) { - // We remove First forward slash from the URL before matching - return getActiveRoute().substring(1) === routePath; -} - /** * Navigate to the route that we originally intended to go to * but the NavigationContainer was not ready when navigate() was called diff --git a/src/libs/Navigation/linkTo.js b/src/libs/Navigation/linkTo.js index 55bd4b31dbdf..ca87a0d7b79c 100644 --- a/src/libs/Navigation/linkTo.js +++ b/src/libs/Navigation/linkTo.js @@ -41,7 +41,7 @@ function getMinimalAction(action, state) { return currentAction; } -export default function linkTo(navigation, path, type) { +export default function linkTo(navigation, path, type, isActiveRoute) { if (navigation === undefined) { throw new Error("Couldn't find a navigation object. Is your component inside a screen in a navigator?"); } @@ -86,7 +86,7 @@ export default function linkTo(navigation, path, type) { // There are situations where a route already exists on the current navigation stack // But we want to push the same route instead of going back in the stack // Which would break the user navigation history - if (type === CONST.NAVIGATION.ACTION_TYPE.PUSH) { + if (!isActiveRoute && type === CONST.NAVIGATION.ACTION_TYPE.PUSH) { minimalAction.type = CONST.NAVIGATION.ACTION_TYPE.PUSH; } // There are situations when the user is trying to access a route which he has no access to