当我在React native中按下导航抽屉中的侧边菜单按钮时不起作用

时间:2018-07-06 16:14:28

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

静态navigationOptions =({navigation})=> {     返回{       headerLeft :(                   {navigation.navigate('DrawerOpen')}} />                       )     }   }

2 个答案:

答案 0 :(得分:2)

由React Navigation(v2)提供。您可以在https://reactnavigation.org/docs/en/drawer-based-navigation.html

中找到文档

您需要在下面这样使用

static navigationOptions = ({ navigation }) => ({
    headerLeft: <Button onPress={() => navigation.toggleDrawer()} />
})

要打开和关闭抽屉,请使用以下助手来打开和关闭抽屉:

this.props.navigation.openDrawer();
this.props.navigation.closeDrawer();

如果要切换抽屉,请调用以下命令:

this.props.navigation.toggleDrawer();

这些功能中的每一个都只是在幕后调度动作:

this.props.navigation.dispatch(DrawerActions.openDrawer());
this.props.navigation.dispatch(DrawerActions.closeDrawer());
this.props.navigation.dispatch(DrawerActions.toggleDrawer());

答案 1 :(得分:0)

这为我解决了导航问题。

  1. 通过运行npm install --save native-base

  2. 安装NativeBase
  3. 运行react-native link链接字体

  4. 导入此import {Header, Left, Icon} from 'native-base'

  5. 将此代码添加到render(){}

    render() {
      return (
        <View>
          <Header>
            <Left>
              <Icon name="menu" style={{ color: 'white'}} 
               onPress={() => this.props.navigation.toggleDrawer()} />
            </Left>
          </Header>
          <View>
            <Text>Home Contents</Text>
          </View>
        </View>
      );
    }