opening/closing 导航抽屉在 React Native 中不工作

opening/closing navigation drawer not working in react native

在我的 React 本机应用程序中,我使用 Drawer Navigator 创建了一个侧边菜单,当我通过滑动打开它时,它工作得很好。但我想做的是在单击按钮时打开它。目前正在尝试做槽式导航道具,相关代码如下:

import { withNavigation } from 'react-navigation';

class HallsList extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      isSideMenuOpen: false
    };
  } 

  renderTopView = () => {
      return(
        <View>
          <View style = {Styles.sideMenuButton}>
            <Button
              onPress = {()=> {
                if (this.state.isSideMenuOpen) {
                  {this.props.navigation.navigate('DrawerOpen')}
                }
                else {
                  {this.props.navigation.navigate('DrawerClose')}
                }
                this.setState({isSideMenuOpen: !this.state.isSideMenuOpen})
              }
            }
            title = {'Side Menu'}
            />
          </View> ..... 


export default withNavigation(HallsList);

但是当我点击侧边菜单按钮时它被点击但之后没有任何反应。

只需更改以下代码部分

而不是this.props.navigation.navigate('DrawerOpen')

this.props.navigation.openDrawer();

而不是this.props.navigation.navigate('DrawerClose')

this.props.navigation.closeDrawer();

您可以简单地使用:

this.props.navigation.toggleDrawer();

而不是:

// to keep track of state of drawer
if (this.state.isSideMenuOpen) {
    {this.props.navigation.navigate('DrawerOpen')}
}
else {
    {this.props.navigation.navigate('DrawerClose')}
}
this.setState({isSideMenuOpen: !this.state.isSideMenuOpen})

打开抽屉使用this.props.navigation.openDrawer() 并关闭抽屉使用 this.props.navigation.closeDrawer()

from this Document