The setup
So you have a structure similar to this:
StackNavigator Home Login Authenticated -> CustomTabNavigator ScreenOne -> StackNavigator ScreenOneDetails ScreenOneSettings ScreenTwo
And you found yourself navigated to Screen Two
.
The problem
How do you navigate from Screen Two
to Home?
The solution(s)
Store your StackNavigator's reference globally, and use it freely wherever you need.
App.js (or wherever you render your StackNavigator
)
render() { return ( <StackNavigator ref={(x) => (global.stackNavigator = x)} />) }
Any component:
global.stackNavigator.dispatch( NavigationActions.navigate({ routeName: 'Home', params: { }, }), );
Disclaimer
This solution is by any means not the recommended way of using navigators. Whenever you can, navigate using the this.props.navigation.navigate()
as described in the official docs:
However, there are cases which the official way doesn't support, such as a navigator with dynamic routes constructed at runtime:
https://reactnavigation.org/docs/4.x/limitations/Follow the entire Github issue here:
https://github.com/react-navigation/react-navigation/issues/335