admin管理员组文章数量:1024582
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 badges1 Answer
Reset to default 0If 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> ); }
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 badges1 Answer
Reset to default 0If 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> ); }
本文标签:
版权声明:本文标题:android - TypeError: navigation.openDrawer is not a function (it is undefined) js engine: hermes in React Native - Stack Overflo 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745621242a2159583.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论