Skip to content

Commit

Permalink
feat: Navigation navigateLater and enableNavigation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kidroca committed Apr 26, 2021
1 parent 103ab13 commit 529f0fb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
29 changes: 27 additions & 2 deletions src/libs/Navigation/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SCREENS from '../../SCREENS';
import CustomActions from './CustomActions';

export const navigationRef = React.createRef();
let lastAttemptedRoute;

/**
* Opens the LHN drawer.
Expand Down Expand Up @@ -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.
*
Expand All @@ -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;
3 changes: 2 additions & 1 deletion src/libs/Navigation/NavigationRoot.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -37,6 +37,7 @@ class NavigationRoot extends Component {
<NavigationContainer
fallback={<FullScreenLoadingIndicator visible />}
onStateChange={this.parseAndStoreRoute}
onReady={Navigation.enableNavigation}
ref={navigationRef}
linking={linkingConfig}
documentTitle={{
Expand Down

0 comments on commit 529f0fb

Please sign in to comment.