8

In my react native app I have created a side menu using Drawer Navigator which is working perfectly when I open it by swiping. But I want to do is to open it on button click. Currently I am trying to do trough navigation props, the related code is as follows:

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);

But when I tap on side menu button it gets tapped but nothing happens afterwards.

4 Answers 4

9

Just change the below code parts

Instead of this.props.navigation.navigate('DrawerOpen')

Put this.props.navigation.openDrawer();

Instead of this.props.navigation.navigate('DrawerClose')

Put this.props.navigation.closeDrawer();

Sign up to request clarification or add additional context in comments.

Comments

2

you can simply use :

this.props.navigation.toggleDrawer();

Instead of:

// 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})

Comments

1

This is working for opening and closing a toggle drawer:

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

Comments

0

to open drawer use this.props.navigation.openDrawer() and close drawer use this.props.navigation.closeDrawer()

from this Document

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.