After Navigating to nested Navigation Screen , cannot go back or view the Nested Navigation Initial route

41 views Asked by At

I have used MainBottomTabNavigation and StackNavigation in my app. This is my navigation Layout

Main Bottom Tab Navigation

  • Dashboard Screen
  • Screen 1 ( Stack Navigation)
    • Screen A (Initial Page)
    • Screen B

In my dashboard screen , I have a component that navigates from Dashboard to Screen B. On first time logging in to app, if navigation is done from Dashboard to Screen B from that component, then I cannot view the screen 1 in the normal navigation flow and only screen B appears.

If I go to screen A initially and then go to the nested navigation from dashboard, everything works fine.

<Tab.Navigator
 initialRouteName="Home">
      <Tab.Screen
        name="Home"
        component={Dashboard}
        options={{tabBarLabel: 'Dashboard'}}
      />
      <Tab.Screen
        name="Screen1"
        component={Screen1}
        options={{tabBarLabel: 'Screen 1', headerShown: false}}
      />
    </Tab.Navigator>

Stack Navigation Screen 1

<Stack.Navigator
  initialRouteName="StackHome">
      <Stack.Screen
        name="ScreenA"
        component={StackHomeScreen}
      />

      <Stack.Screen name="ScreenB" component={ScreenBScreen} />


    </Stack.Navigator>

Nested Navigation Code

          <TouchableOpacity
            onPress={() => {
              navigation.navigate('StackHome', {
                screen: 'ScreenB',
              });
            }}>

React Navigation Versions

    "@react-navigation/bottom-tabs": "^6.5.11",
    "@react-navigation/material-top-tabs": "^6.6.6",
    "@react-navigation/native": "^6.1.9",
    "@react-navigation/native-stack": "^6.9.17",
3

There are 3 answers

0
jihadrobb On

Try using CommonActions.reset instead of navigate

import { CommonActions } from '@react-navigation/native';

navigation.dispatch(
  CommonActions.reset({
    index: 1,
    routes: [
      {
        name: 'StackHome',
        params: { screen: 'ScreenA' },
      },
      {
        name: 'StackHome',
        params: { screen: 'ScreenB' },
      },
    ],
  })
);
0
Ibrahim Khan On

navigation.push('StackHome', {
 screen: 'ScreenB',
});

Try using push instead of navigate

0
Pratik Prakash Bindage On
  • When you navigate from the dashboard to screen B, you're not specifying the parent stack (Screen1) in the navigation route. This is why you're only able to see screen B and not the parent stack.

  • You need to specify the parent stack (Screen1) in the navigation route.You can do this by using the navigate function with the key parameter. The key parameter is used to specify the parent stack.

    <TouchableOpacity
     onPress={() => {
      navigation.navigate('StackHome', {
       screen: 'ScreenB',
       key: 'Screen1', // Add this line to specify the parent stack
      });
    }}>