diff --git a/src/libs/Navigation/Navigation.js b/src/libs/Navigation/Navigation.js index 28dbfe330da4..a5bd681c2115 100644 --- a/src/libs/Navigation/Navigation.js +++ b/src/libs/Navigation/Navigation.js @@ -9,6 +9,7 @@ import SCREENS from '../../SCREENS'; import CustomActions from './CustomActions'; export const navigationRef = React.createRef(); +let lastAttemptedRoute; /** * Opens the LHN drawer. @@ -91,6 +92,27 @@ function dismissModal(shouldOpenDrawer = false) { openDrawer(); } +/** + * Capture any navigation attempts until the navigation container and all it's children finish mounting + * + * @param {String} route + */ +function navigateLater(route) { + lastAttemptedRoute = route; +} + +/** + * Enable navigation after the navigation tree becomes ready + * Executes the last captured navigation command if attempts were made prior being ready + */ +function enableNavigation() { + if (lastAttemptedRoute) { + navigate(lastAttemptedRoute); + } + // eslint-disable-next-line no-use-before-define + exports.navigate = navigate; +} + /** * Determines whether the drawer is currently open. * @@ -100,9 +122,12 @@ function isDrawerOpen() { return getIsDrawerOpenFromState(navigationRef.current.getRootState().routes[0].state); } -export default { - navigate, +const exports = { + navigate: navigateLater, + enableNavigation, dismissModal, isDrawerOpen, goBack, }; + +export default exports; diff --git a/src/libs/Navigation/NavigationRoot.js b/src/libs/Navigation/NavigationRoot.js index 322b7036914f..7cb7be9e8027 100644 --- a/src/libs/Navigation/NavigationRoot.js +++ b/src/libs/Navigation/NavigationRoot.js @@ -1,7 +1,7 @@ import React, {Component} from 'react'; import PropTypes from 'prop-types'; import {getPathFromState, NavigationContainer} from '@react-navigation/native'; -import {navigationRef} from './Navigation'; +import Navigation, {navigationRef} from './Navigation'; import linkingConfig from './linkingConfig'; import AppNavigator from './AppNavigator'; import {setCurrentURL} from '../actions/App'; @@ -37,6 +37,7 @@ class NavigationRoot extends Component { } onStateChange={this.parseAndStoreRoute} + onReady={Navigation.enableNavigation} ref={navigationRef} linking={linkingConfig} documentTitle={{