[英]React-Native Undefined is not an object (evaluating 'this.refs.['DRAWER'])
如何解決此錯誤? 我試過了但是還是出錯..
這是我的代碼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.