React-Native Undefined不是一个对象(评估' this.refs。[' DRAWER'])

时间:2017-03-21 08:20:11

标签: javascript android react-native native-base

如何解决此错误?我已经尝试但仍然出错...

DrawerLayoutAndroid

这是我的代码root.js:

<DrawerLayoutAndroid
    drawerWidth={250}
    drawerPosition={DrawerLayoutAndroid.positions.Left}
    renderNavigationView={() => navigationView}
    ref={'DRAWER'}>
      <Navigator
        renderScene={this.renderScene}
        initialRoute={{component: Home}}
        ref={(nav) => { this.appNav = nav; }}  />
  </DrawerLayoutAndroid>

这是我在Home.js中的代码:

handleMenuPress() {
this.refs['DRAWER'].openDrawer();

}

<Header searchBar rounded>
     <Item>
         <Icon name="menu" onPress={this.handleMenuPress}/>
         <Input placeholder="Cari Rumah Sakit" />
         <Icon active name="search" />
      </Item>
      <Button transparent>
          <Text>Search</Text>
       </Button>
 </Header>

2 个答案:

答案 0 :(得分:1)

可能this不在handleMenuPress的范围内。 尝试将handleMenuPress更改为箭头功能。

handleMenuPress = () => {
  this.refs['DRAWER'].openDrawer();
}

答案 1 :(得分:0)

你应该在构造函数中绑定方法(这是最好的方法)。

constructor(props){
    super(props)
    this.handleMenuPress = this.handleMenuPress.bind(this)
}