如何在我的屏幕上添加React-Navigation Drawer菜单?

时间:2019-04-04 05:08:54

标签: react-native react-native-android react-navigation react-navigation-drawer

我有一个用React-Navigation 3.x构建的主屏幕。我有一个标题,一些导航图标和一个底部标签菜单。它运行良好,但是现在我想在屏幕上添加一个抽屉菜单,并在右上角添加一个图标来切换抽屉。

这是我的主屏幕(App.js)的简化版:

import { createStackNavigator, createAppContainer, createBottomTabNavigator } from 'react-navigation';
import Activity1 from './Activity/Activity1';
import Activity2 from './Activity/Activity2';
import Activity3 from './Activity/Activity3';
import Calendar from './Screens/Calendar';
import Graph from './Screens/Graph';
import DrawerMenu from './components/DrawerMenu';

class HomeScreen extends React.Component {
   static navigationOptions = {
      title: 'Home',
      headerRight: (<Button onPress={() => this.props.navigation.toggleDrawer()} title='Menu' />)
    };

    render() {
       return (
          <View style={styles.container}>
             <View style={styles.iconContainer}>
                <Icon name='icon1' onPress={this.navToActivity1} />
                <Icon name='icon2' onPress={this.navToActivity2} />
                <Icon name='icon3' onPress={this.navToActivity3} />
             </View>
             <View>
                <DrawerMenu />
             </View>
          </View>
       );
    }
}

//create my main navigation stacks here 
const Home = createStackNavigator({
   HomeScreen,
   Activity1,
   Activity2,
   Activity3,
});

//The following two are for the bottom tab bar only
const Calendar = createStackNavigator({ Calendar });
const Graph = createStackNavigator({ Graph });

const BottomTabNav = createBottomTabNavigator({
    Home, Calendar, Graph
});

export default createAppContainer(TabNavigator);

这是DrawerMenu.js的代码

import { Dimensions } from 'react-native';
import { createDrawerNavigator, createAppContainer } from 'react-navigation';
import Settings from './Settings';
import Profile from './Profile';

const SCREENWIDTH = Dimensions.get('window').width;

const DrawerConfig = {
  drawerWidth: SCREENWIDTH * 0.5,
  drawerPosition: 'right',
};

const DrawerMenu = createDrawerNavigator({
       Settings: { screen: Settings },
       Profile: { screen: Profile },
   },
   DrawerConfig
);

export default createAppContainer(DrawerMenu);

我无法让抽屉上班。当我单击主屏幕右上角的“菜单”按钮以调用toggleDrawer()时,出现“未定义不是对象(正在评估'ae.props.navigation')”错误。

也不能使用手势来激活抽屉,所以我认为我没有正确添加它。我在这里做错了什么?谢谢!

1 个答案:

答案 0 :(得分:-1)

将软件包安装为react-native-drawer https://www.npmjs.com/package/react-native-drawer

<Drawer
    type="overlay"
    ref={ref => (this._drawer = ref)}
    content={<Sidebar />}
    tapToClose={true}
    openDrawerOffset={0.2} // 20% gap on the right side of drawer
    panCloseMask={0.2}
    closedDrawerOffset={-3}
    styles={drawerStyles}
    tweenHandler={ratio => ({ main: { opacity: (2 - ratio) / 2 } })}
  >

光盘