在react native中,当抽屉嵌套在堆栈导航中时,如何在抽屉菜单图标上设置操作



我创建了一个堆栈导航&抽屉导航。抽屉导航嵌套在堆栈导航中,因为起初当用户第一次打开应用程序时,他们会看到漫游页面,然后进入包含抽屉导航的主页。

问题是,我在Drawer.Navigator的屏幕选项中的headerLeft中添加了图像,然后点击它,称为navigation.openDrawer((,但似乎navigation.oopenDrawer不是一个函数。

我想要的只是,我想在标题中添加图像,就像打开抽屉的菜单一样;的其他2张图片转到其他页面,应用程序徽标位于中心。

下面是代码&完整视图的expo链接示例:

**APP.JS**
if(isFirstTimeLoad) return (
<>
<StatusBar  />
<Walkthrough onDone={handleDone} />
</>
);
if(!isFirstTimeLoad) return (
<NavigationContainer>
<MainNavigator />
</NavigationContainer>
)
**MAINNAVIGATOR.JS**
<Stack.Navigator>
<Stack.Screen name="Home" component={DrawerNavigator} />
</Stack.Navigator>
**DRAWERNAVIGATOR.JS**
<Drawer.Navigator
useLegacyImplementation
screenOptions={{
headerLeft: () => (
<TouchableOpacity onPress={openDrawer}>
<Image
source={require('../assets/menu.png')}
style={{ height: 20, width: 20 }}
/>
</TouchableOpacity>
),
}}>
<Drawer.Screen name="Feed" component={Feed} options={{}} />
<Drawer.Screen name="Article" component={Article} />
</Drawer.Navigator>

这是方便的完整源代码,博览会链接:点击这里

import { useNavigation,DrawerActions } from '@react-navigation/native';
export default function DrawerNavigator(props) {
// const {navigation} = props
const navigation = useNavigation();
const openDrawer = () => navigation.dispatch(DrawerActions.openDrawer());
return (
<Drawer.Navigator
useLegacyImplementation
screenOptions={{
headerLeft: () => (
<TouchableOpacity onPress={openDrawer}>
<Image
source={require('../assets/menu.png')}
style={{ height: 20, width: 20 }}
/>
</TouchableOpacity>
),
}}>
<Drawer.Screen name="Feed" component={Feed} options={{}} />
<Drawer.Screen name="Article" component={Article} />
</Drawer.Navigator>
);
}

https://snack.expo.dev/hyt2VDjqB

请试试这个:(这个工作

希望它能有所帮助,放心吧

最新更新