最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

android - TypeError: navigation.openDrawer is not a function (it is undefined) js engine: hermes in React Native - Stack Overflo

matteradmin5PV0评论

I'm using React Navigation in my React Native project with a combination of Stack Navigator and Drawer Navigator, but when I try to use navigation.openDrawer() in my component, I'm getting the following error:

  • ERROR TypeError: navigation.openDrawer is not a function (it is undefined), js engine: hermes

Code Overview:

Home.js

import { useNavigation } from '@react-navigation/native';<br>
const navigation = useNavigation();

const LeftIcon = () => {
    return (
      <TouchableOpacity
        style={styles.pr15}
        onPress={() => navigation.openDrawer()}>
          <Ionicons
          name="menu"
          color={colors.textColor}
          size={moderateScale(30)}
        />
      </TouchableOpacity>
    );
  };

<LeftIcon />

NavigationKeys.js

export const TabNav = {
  Home: 'Home',
  Profile: 'Profile',
  Library: 'Library',
  Explore: 'Explore',
  DrawerNavigation: 'DrawerNavigation',
};
export const DrawerNav = {
  Home: 'Home',
  Profile: 'Profile',
  Library: 'Library',
  Explore: 'Explore',
};

NavigationRoutes.js

import DrawerNavigation from './Type/DrawerNavigation';

export const TabRoute = {
  Home,
  Profile,
  Library,
  Explore,
  DrawerNavigation,
};
export const DrawerRoute = {
  Home,
  Profile,
  Library,
  Explore,
};

StackNavigation.js

<Stack.Screen
        name={TabNav.DrawerNavigation}
        component={TabRoute.DrawerNavigation}
      />

I’ve tried using navigation.dispatch(DrawerActions.openDrawer()) from @react-navigation/drawer instead of navigation.openDrawer(), but I still get the same error.

I'm using React Navigation in my React Native project with a combination of Stack Navigator and Drawer Navigator, but when I try to use navigation.openDrawer() in my component, I'm getting the following error:

  • ERROR TypeError: navigation.openDrawer is not a function (it is undefined), js engine: hermes

Code Overview:

Home.js

import { useNavigation } from '@react-navigation/native';<br>
const navigation = useNavigation();

const LeftIcon = () => {
    return (
      <TouchableOpacity
        style={styles.pr15}
        onPress={() => navigation.openDrawer()}>
          <Ionicons
          name="menu"
          color={colors.textColor}
          size={moderateScale(30)}
        />
      </TouchableOpacity>
    );
  };

<LeftIcon />

NavigationKeys.js

export const TabNav = {
  Home: 'Home',
  Profile: 'Profile',
  Library: 'Library',
  Explore: 'Explore',
  DrawerNavigation: 'DrawerNavigation',
};
export const DrawerNav = {
  Home: 'Home',
  Profile: 'Profile',
  Library: 'Library',
  Explore: 'Explore',
};

NavigationRoutes.js

import DrawerNavigation from './Type/DrawerNavigation';

export const TabRoute = {
  Home,
  Profile,
  Library,
  Explore,
  DrawerNavigation,
};
export const DrawerRoute = {
  Home,
  Profile,
  Library,
  Explore,
};

StackNavigation.js

<Stack.Screen
        name={TabNav.DrawerNavigation}
        component={TabRoute.DrawerNavigation}
      />

I’ve tried using navigation.dispatch(DrawerActions.openDrawer()) from @react-navigation/drawer instead of navigation.openDrawer(), but I still get the same error.

Share Improve this question edited Nov 18, 2024 at 12:34 RRC asked Nov 18, 2024 at 11:32 RRCRRC 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0
  • If you're trying to open the drawer from a component that doesn't have direct access to the drawer navigation, you can use DrawerActions to trigger an action to open the drawer

    import { DrawerActions } from '@react-navigation/native';
    
    // Update in your component
    const LeftIcon = () => {
    const navigation = useNavigation(); // Confirm this is inside the component
    
     return (
       <TouchableOpacity style={styles.pr15}
         onPress={() => navigation.dispatch(DrawerActions.openDrawer())}>
         <Ionicons name="menu" color={colors.textColor} size={moderateScale(30)} />
       </TouchableOpacity>
     );
    };
    
    export default LeftIcon;
    
  • Updated navigation code

    import { NavigationContainer } from '@react-navigation/native';
    import { createDrawerNavigator } from '@react-navigation/drawer';
    import { createStackNavigator } from '@react-navigation/stack';
    import HomeScreen from './Home';
    import ProfileScreen from './Profile';
    
    const Stack = createStackNavigator();
    const Drawer = createDrawerNavigator();
    
    function MyStack() {
      return (
        <Stack.Navigator>
          <Stack.Screen name="Home" component={HomeScreen} />
          <Stack.Screen name="Profile" component={ProfileScreen} />
        </Stack.Navigator>
      );
    }
    
    function MyDrawer() {
      return (
        <Drawer.Navigator>
          <Drawer.Screen name="Main" component={MyStack} />
          {/* Other drawer screens */}
        </Drawer.Navigator>
      );
    }
    
    export default function App() {
      return (
       <NavigationContainer>
         <MyDrawer />
       </NavigationContainer>
     );
    }
    

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far