From 529f0fb6ff0472beee208de6a5ab6320917a4dc8 Mon Sep 17 00:00:00 2001 From: Peter Velkov Date: Mon, 26 Apr 2021 18:16:21 +0300 Subject: [PATCH] feat: Navigation navigateLater and enableNavigation Wait for the NavigationContainer and all its children finish mounting for the first time before allowing/performing navigation If there were navigation attempts - the last navigation attempt would be automatically applied when the navigation is enabled --- src/libs/Navigation/Navigation.js | 29 +++++++++++++++++++++++++-- src/libs/Navigation/NavigationRoot.js | 3 ++- 2 files changed, 29 insertions(+), 3 deletions(-) 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={{