Search code examples
react-nativereact-navigationreact-navigation-stackreact-navigation-bottom-tabreact-navigation-v6

Why I can't navigate using bottom navigator tabs?


I have a bottom navigator tab which is showing correctly but when I click on any of the tabs it does not do anything:

import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
const Stack = createStackNavigator();
const Tab = createBottomTabNavigator();


function HomeScreen() {       
   return (
      <Tab.Navigator>
         <Tab.Screen name="Home" component={Home} />
         <Tab.Screen name="Scan" component={Scan} initialParams={{ type: '' }} />
         <Tab.Screen name="StockStatus" component={StockStatus} />
      </Tab.Navigator>
   );
}


  function navigation() {
   return (
      <NavigationContainer>
         <Stack.Navigator>
            <Stack.Screen name="Login" component={Login}
               options={{
                  headerShown: false,
               }} />
            <Stack.Screen
               name="homeScreen"
               component={HomeScreen}
               options={{ headerShown: false }}
            />           
         </Stack.Navigator>
      </NavigationContainer>
   )}

It does not display any warning nor errors. So what could be the problem?!


Solution

  • I can't find anything wrong at you code. I wanna provide a working example.

    import { View, Text, Button } from 'react-native';
    import { NavigationContainer, useNavigation } from '@react-navigation/native';
    import { createStackNavigator } from '@react-navigation/stack';
    import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
    
    const Stack = createStackNavigator();
    const Tab = createBottomTabNavigator();
    
    function Dummy({ title }) {
      return (
        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
          <Text>{title}</Text>
        </View>
      );
    }
    
    function Login() {
      const navigation = useNavigation();
    
      return (
        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
          <Text>Login</Text>
          <Button
            title={'GOTO Tab'}
            onPress={() => navigation.navigate('homeScreen')}
          />
        </View>
      );
    }
    
    const Home = () => <Dummy title="Home" />;
    const Scan = () => <Dummy title="Scan" />;
    const StockStatus = () => <Dummy title="StockStatus" />;
    
    function HomeScreen() {
      return (
        <Tab.Navigator>
          <Tab.Screen name="Home" component={Home} />
          <Tab.Screen name="Scan" component={Scan} />
          <Tab.Screen name="StockStatus" component={StockStatus} />
        </Tab.Navigator>
      );
    }
    
    export default function navigation() {
      return (
        <NavigationContainer>
          <Stack.Navigator>
            <Stack.Screen
              name="Login"
              component={Login}
              options={{
                headerShown: false,
              }}
            />
            <Stack.Screen
              name="homeScreen"
              component={HomeScreen}
              options={{ headerShown: false }}
            />
          </Stack.Navigator>
        </NavigationContainer>
      );
    }