root-two / react-native-drawer

React Native Drawer
MIT License
2.54k stars 390 forks source link

undefined is not an object (evaluating _this2_.drawer.open) #300

Open sclanc opened 7 years ago

sclanc commented 7 years ago

Having followed the usage docs, and examples very carefully I am lost as to where I went wrong here, Do the docs need to be updated? What am I doing wrong?

import React, { Component } from 'react';
import Drawer from 'react-native-drawer'
import {
   AppRegistry,
   StyleSheet,
   Text,
  View,
   Button
 } from 'react-native';

 class Project extends Component {
   render () {
     return (
       <Drawer
         ref={(ref) => this._drawer = ref}
         content={<Menu
                    close={() => {this._drawer.close()}}
                   />}
         >
        <Main
          open={() => {this._drawer.open()}}
          />
       </Drawer>
     )
   }
 }

 class Menu extends Component {
   render() {
     return (
       null
     );
   }
 }

 class Main extends Component {
   render() {
     return (
      <View>
        <Button
          title='Open'
          onPress={this.props.open()}
          />
       </View>
     )
   }
 }
 AppRegistry.registerComponent('Project', () => Project);
dopey2 commented 7 years ago

try with:

` <Drawer ref="myDrawer"

` then you open/close it like this:

this.refs.myDrawer.open() this.refs.myDrawer.close()

ChirantanPatel commented 6 years ago

Setting.js

closeDrawer = () => {
    this.drawer._root.close()
  };
openDrawer = () => {
    //alert("open");
    debugger
    this.drawer._root.open()
  };

<Drawer ref={(ref) => { this.drawer = ref; }} content={} onClose={() => this.closeDrawer()} >

    <Container>

        <NavBar openDrawermethod={()=>this.openDrawer()}/>

    </Container>
    </Drawer>

NavBar.js

Header

Home.js

constructor(props) { super(props) _Setting = new Setting(); }

' <NavBar openDrawermethod={()=> _Setting.openDrawer()} />

            <Bar></Bar>
            <PhotoGrid></PhotoGrid>
            <FooterClass> </FooterClass>
        </View>'

I getting this type error Error: Cannot read property '_root' of undefined. undefined is not an object (evaluating '_this.drawer._root')