Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native programmatically open DrawerLayoutAndroid

Tags:

react-native

I am using the DrawerLayoutAndroid and struggling to work out how to programmatically open and close it.

I see that you are supposed to use

openDrawer(0)but how do you reference the DrawerLayoutAndroid

I have a drawer that I have made exactly like in the docs, then I have a button on my View that if you press it I want it to open the DrawerLayoutAndroid

I created this function

toggleDrawer(){
  openDrawer(0);
};

but that obviously doesnt work, and just throws an error.

like image 640
Adam Katz Avatar asked Aug 26 '26 11:08

Adam Katz


1 Answers

You should use refs for this. I am pasting the example here for your reference

    var DrawerExample = React.createClass({

        openDrawer:function() {
            this.refs['myDrawer'].openDrawer();
        },

        closeDrawer:function() {
            this.refs['myDrawer'].closeDrawer();
        },

        render: function() {
            var navigationView = (
                <View style={{flex: 1, backgroundColor: '#fff'}}>
                    <Text style={{margin: 10, fontSize: 15, textAlign: 'left'}}>I'm in the Drawer!</Text>

                    <TouchableHighlight onPress={this.closeDrawer}>
                        <Text>{'Close Drawer'}</Text>
                    </TouchableHighlight>

                </View>
            );
            return (
                <DrawerLayoutAndroid ref="myDrawer"
                    drawerWidth={300}
                    drawerPosition={DrawerLayoutAndroid.positions.Left}
                    renderNavigationView={() => navigationView}>
                    <View style={{flex: 1, alignItems: 'center'}}>
                        <Text style={{margin: 10, fontSize: 15, textAlign: 'right'}}>Hello</Text>
                        <Text style={{margin: 10, fontSize: 15, textAlign: 'right'}}>World!</Text>

                        <TouchableHighlight onPress={this.openDrawer}>
                            <Text>{'Open Drawer'}</Text>
                        </TouchableHighlight>

                    </View>
                </DrawerLayoutAndroid>
            );
        },
    });
like image 145
Jagadish Upadhyay Avatar answered Aug 29 '26 19:08

Jagadish Upadhyay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!