Skip to content

Instantly share code, notes, and snippets.

@TiagoGouvea
Created October 10, 2015 16:12
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save TiagoGouvea/ba30975dd125d8a358ae to your computer and use it in GitHub Desktop.
Save TiagoGouvea/ba30975dd125d8a358ae to your computer and use it in GitHub Desktop.
Sample DrawerLayout opened by clicking in a TouchableHighlight component
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
DrawerLayoutAndroid,
TouchableHighlight,
} = React;
var DrawerTest = React.createClass({
openDrawer:function() {
this.refs['DRAWER'].openDrawer()
},
render: function() {
var navigationView = (
<View style={{flex: 1, backgroundColor: '#fff'}}>
<Text style={{margin: 10, fontSize: 15, textAlign: 'left'}}>I'm in the Drawer!</Text>
</View>
);
return (
<DrawerLayoutAndroid
drawerWidth={300}
ref={'DRAWER'}
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>
);
}
});
AppRegistry.registerComponent('DrawerTest', () => DrawerTest);
@yashvekaria
Copy link

hi, I wanted to implement same functionality as above but I am getting error of an object.

Error: undefined is not an object (evaluating 'this.refs['DRAWER]')

Here is my code snippet:

class LoginPage extends Component {

    openDrawer() {
        this.refs['DRAWER'].openDrawer()
    }
    render() {
        var navigationView = (
            <SideBarMenu />
        );
        return (
            <DrawerLayoutAndroid
              drawerWidth={300}           
              ref={'DRAWER'}
              drawerPosition={DrawerLayoutAndroid.positions.Left}
              renderNavigationView={() => navigationView}>               
              <View style={{flex: 1, alignItems: 'center'}}>                
                <TouchableHighlight onPress={this.openDrawer}>
                  <Text>{'Open Drawer'}</Text>
                </TouchableHighlight>
              </View>
              <ComponentDemo />
            </DrawerLayoutAndroid>
        );
    }
}

Can you please guide me where am I am wrong.

@ajeshekl
Copy link

I had the same issue.
Just Try <TouchableHighlight onPress={()=>this.openDrawer()}> instead of <TouchableHighlight onPress={this.openDrawer}>.
This will fix your issue......

@lalkmim
Copy link

lalkmim commented Jul 19, 2016

This question is a little old now, but I just got into the same problem. A few notes:

Using ref with string is deprecated, you should use a callback function. Also, with classes, you should bind the function in the constructor so it gets the correct scope.

Didn't test, but I believe below code should work:

class LoginPage extends Component {
    constructor() {
        super();
        this.openDrawer = this.openDrawer.bind(this);
    }

    openDrawer() {
        this.drawer.openDrawer();
    }

    render() {
        return (
            <DrawerLayoutAndroid
              drawerWidth={300}           
              ref={(_drawer) => this.drawer = _drawer}
              drawerPosition={DrawerLayoutAndroid.positions.Left}
              renderNavigationView={() => <SideBarMenu />}>               
              <View style={{flex: 1, alignItems: 'center'}}>                
                <TouchableHighlight onPress={this.openDrawer}>
                  <Text>{'Open Drawer'}</Text>
                </TouchableHighlight>
              </View>
              <ComponentDemo />
            </DrawerLayoutAndroid>
        );
    }
}

@Dssdiego
Copy link

Thanks @lalkmim! It worked!

@GuGuss
Copy link

GuGuss commented Dec 13, 2016

Sad we can't 👍 on gist! Thanks @lalkmim for your clean solution.

@anilgudur
Copy link

anilgudur commented May 5, 2017

Thanks @lalkmim for the solution!

@johngoren
Copy link

Thanks @Ialkmim -- but in order to make it work, I had to change this.drawer.openDrawer() to this._drawer.openDrawer()...

@Keshava11
Copy link

@ajeshekl Thanks. Worked for me. Is it due to change in ES standard ?

@shiundu
Copy link

shiundu commented Jan 10, 2018

Thanks @yashvekaria for raising the issue and @lalkmim and @ajeshekl for the solution!

@shures
Copy link

shures commented Mar 25, 2019

I got an issue, it shows only shadows when clicking the bottom
but shows navigation view when swiped from left

@JexelJimenez
Copy link

I got an issue, it shows only shadows when clicking the bottom
but shows navigation view when swiped from left

same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment