Having two sets of stack navigators;
const SetOneScreens = () => (
<Stack.Navigator initialRouteName="AScreen">
<Stack.Screen name="AScreen" component={AScreen} />
<Stack.Screen name="BScreen" component={BScreen} />
</Stack.Navigator>
);
const SetTwoScreens = () => (
<Stack.Navigator initialRouteName="CScreen">
<Stack.Screen name="CScreen" component={CScreen} />
<Stack.Screen name="DScreen" component={DScreen} />
</Stack.Navigator>
);
Which are nested in a Drawer navigator
<NavigationContainer>
<Drawer.Navigator initialRouteName="SetOneScreens">
<Drawer.Screen name="SetOneScreens" component={SetOneScreens} />
<Drawer.Screen name="SetTwoScreens" component={SetTwoScreens} options={{swipeEnabled: false}} />
</Drawer.Navigator>
</NavigationContainer>
I want to navigate From BScreen
to DScreen
and reset the stack (in order to not letting hardware back button in android get back to BScreen
)
As in the nesting situation, we should first define the navigator name; how should I define the screen in reset action.
// For navigation
props.navigation.navigate('setTwoScreens',{screen:'DScreen'})
// For reset I can only navigate to initial screen
props.navigation.reset({index:0,routes:[{name:'setTwoScreens'}]})
How should I handle the reset
with navigation
or CommonActions
“Nesting navigator means rendering a navigator inside a screen of another navigator” ( React Navigation Documentation) One project that I’ve worked with had the tab navigator as the topmost navigator such as below: The Home, Profile, and Settings screens are to be part of the tab bar; Each of them has their own stack navigator.
Another example is having a tab navigator to display the application main screens and then using a stack navigator to show more screens that relate to each tab. “Nesting navigator means rendering a navigator inside a screen of another navigator” ( React Navigation Documentation)
But this is going to reset your navigation state with completely new state, unmounting and remounting some screens. You can keep the old routes like shown in dispatch docs but it only applies to current navigator, not child navigator. Here, it seems like you just want navigate, not reset.
Other actions such as navigate work similarly, i.e. navigation will happen in the nested navigator and if the nested navigator couldn't handle it, then the parent navigator will try to handle it.
As written in the documentation of react-navigation-v5, you need to dispatch CommonAction with reset-action to clear back-stack of your application, so that application doesn't go back to previous screen when user press hardware back-button of device, check below example,
import { CommonActions } from "@react-navigation/native";
props.navigation.dispatch({
...CommonActions.reset({
index: 0,
routes: [{ name: "AnotherStackNavigator" }]
})
});
Or if you want to reset to specific screen in that StackNavigator you can do like this:
props.navigation.dispatch({
...CommonActions.reset({
index: 0,
routes: [
{
name: "AnotherStackNavigator",
state: {
routes: [
{
name: "AnotherStackNavigatorScreen",
params: {
...
}
}
]
}
}
]
})
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With