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

标签 react-native stack-navigator react-navigation-drawer

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

问题在于,我在 Drawer.Navigator 的 screenOptions 的 headerLeft 中添加了图像,然后单击它,调用了 navigation.openDrawer(),但似乎 navigation.openDrawer 不是一个函数。

我想要的是,我想在标题中添加图像,例如打开抽屉的菜单,以及其他 2 个用于转到其他页面的图像和中心的应用程序 Logo 。

下面是完整 View 示例的代码和博览会链接:

**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>

为了方便起见,这里是完整的源代码,博览会链接:click here

最佳答案

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

请尝试一下:)这有效

希望对你有帮助,如有疑问请放心

关于react-native - 当抽屉嵌套在 native 堆栈导航中时,如何在抽屉菜单图标上设置操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73617040/

相关文章:

react-native - StyleSheet.create 的意义是什么

android - StackNavigator 中的 TabNavigator - React Native

reactjs - (React-Native) undefined 不是一个对象(评估 _'this.props.navigation.navigate' )

javascript - 如何在 native react 中将 stackNavigation 与抽屉导航一起使用并在 stackNavigation 组件中使用toggleDrawer()

react-native - React Navigation 6.x 按后退按钮带你回到初始屏幕

node.js - Chrome 获取 Set-cookie header 但未将其放入新请求中

react-native - iOS 上的 Expo 崩溃而没有错误

reactjs - 我们如何在 React 中部分更新状态?

react-native - 如何在 CustomDrawerContent 中使用 props.navigation.closeDrawer() ?(React Native)("@react-navigation/drawer": "^5.11.4")

javascript - 在 React Native 中哪里调用 addListener