繁体   English   中英

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

[英]React-Native Undefined is not an object (evaluating 'this.refs.['DRAWER'])

如何解决此错误? 我试过了但是还是出错..

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>

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

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

您应该在构造函数内部绑定方法(这是最佳方法)。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM