react-native - React 导航打开 openDrawer() 不起作用

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

这是我的导航代码,我有一个抽屉式导航器,其中包含堆栈,我遇到的问题是我无法使用 this.props.navigation.openDrawer()在堆栈中,导航器打开抽屉,但我仍然可以通过在屏幕上滑动来打开抽屉。我的代码,

const MyDrawerNavigator = createDrawerNavigator(
  {
    Home: AppStack,
    MyAccount: MyAccountStack,
    PointsProfile: PointsProfStack,
    WishList: WishListStack,
    BonusPoint: BonusPoint,
    ContactUs: ContactUs,
    InviteFriend: InviteFriend,
    Terms: Terms,
    SignOut: SignOut
  }
}


const AppStack = createStackNavigator(
  {
    Home: Home,
    Notification: Notification,
    Suggested: Suggested,
    HomeSearch: HomeSearchV2,
    SearchHist: DishSearchHistory,
    //tab screens
    MealScreen: MealScreenTab,
    SearchScreen: SearchScreenTab,
    CuisineScreen: CuisineScreenTab
})

当我在 AppStack 中控制台记录 this.props.navigation 时,我发现未提供 openDrawer() 函数。 但是,当我在 ContactUs(只是一个屏幕)中控制台记录 this.props.navigation 时,它显示 openDrawer() 函数。

我编写导航的方式是否错误,任何帮助将不胜感激。

提前致谢。

最佳答案

这里您可以引用抽屉式导航栏的代码。

import React, { Component } from 'react';
import { View, Image, TouchableOpacity } from 'react-native';
import {
  createDrawerNavigator,
  createStackNavigator,
  createAppContainer,
} from 'react-navigation';

import Screen1 from './pages/Screen1';
import Screen2 from './pages/Screen2';
import Screen3 from './pages/Screen3';

class NavigationDrawerStructure extends Component {
  //Structure for the navigatin Drawer
  toggleDrawer = () => {
    //Props to open/close the drawer
    this.props.navigationProps.toggleDrawer();
  };
  render() {
    return (
      <View style={{ flexDirection: 'row' }}>
        <TouchableOpacity onPress={this.toggleDrawer.bind(this)}>
          {/*Donute Button Image */}
          <Image
            source={require('./image/drawer.png')}
            style={{ width: 25, height: 25, marginLeft: 5 }}
          />
        </TouchableOpacity>
      </View>
    );
  }
}

const FirstActivity_StackNavigator = createStackNavigator({
  //All the screen from the Screen1 will be indexed here
  First: {
    screen: Screen1,
    navigationOptions: ({ navigation }) => ({
      title: 'Demo Screen 1',
      headerLeft: <NavigationDrawerStructure navigationProps={navigation} />,
      headerStyle: {
        backgroundColor: '#FF9800',
      },
      headerTintColor: '#fff',
    }),
  },
});

const Screen2_StackNavigator = createStackNavigator({
  //All the screen from the Screen2 will be indexed here
  Second: {
    screen: Screen2,
    navigationOptions: ({ navigation }) => ({
      title: 'Demo Screen 2',
      headerLeft: <NavigationDrawerStructure navigationProps={navigation} />,
      headerStyle: {
        backgroundColor: '#FF9800',
      },
      headerTintColor: '#fff',
    }),
  },
});

const Screen3_StackNavigator = createStackNavigator({
  //All the screen from the Screen3 will be indexed here
  Third: {
    screen: Screen3,
    navigationOptions: ({ navigation }) => ({
      title: 'Demo Screen 3',
      headerLeft: <NavigationDrawerStructure navigationProps={navigation} />,
      headerStyle: {
        backgroundColor: '#FF9800',
      },
      headerTintColor: '#fff',
    }),
  },
});

const DrawerNavigatorExample = createDrawerNavigator({
  //Drawer Options and indexing
  Screen1: {
    //Title
    screen: FirstActivity_StackNavigator,
    navigationOptions: {
      drawerLabel: 'Demo Screen 1',
    },
  },
  Screen2: {
    //Title
    screen: Screen2_StackNavigator,
    navigationOptions: {
      drawerLabel: 'Demo Screen 2',
    },
  },
  Screen3: {
    //Title
    screen: Screen3_StackNavigator,
    navigationOptions: {
      drawerLabel: 'Demo Screen 3',
    },
  },
});

export default createAppContainer(DrawerNavigatorExample);

祝你有美好的一天。

关于react-native - React 导航打开 openDrawer() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56862841/

相关文章:

android - react native 。安卓。推送通知权限

javascript - 类型错误 : Cannot read property 'prototype' of undefined in jest test

android - 我可以从工具栏中删除汉堡包(抽屉导航图标),删除它的区域(矩形)吗?

javascript - react native 嵌套堆栈导航显示空屏幕

react-native - react 导航 v5 : How to use with Branch. io

javascript - createMaterialTopTabNavigator 接受事件背景色

javascript - 捆绑失败: Error: Requires Babel "^7.0.0-0"

javascript - 如何在 Material ui react 中的makeStyles中使用状态值

android - ActionBarDrawerToggle 没有合适的构造函数可绘制

ListView上的Android DrawerLayout背景选择