[英]I got the error when calling a function with param
onTypeSelect (str) {
this.setState({taskType: str})
}
<TouchableOpacity onPress={this.onTypeSelect('type1').bind(this)}>
</TouchableOpacity>
undefined不是對象(評估'_this3.onTypeSelect('type1).bind')。
誰能幫我? 我是react-native的初學者。
嘗試這個
onTypeSelect (str) {
this.setState({taskType: str})
}
<TouchableOpacity onPress={() => { this.onTypeSelect('type1'); }} />
.bind(this)時, 您需要傳遞一個函數而不是函數的返回值,在這種情況下為undefined
。 這樣,它的工作原理,因為函數是由從而使組件范圍內叫this
有效的參考。
解決該問題的另一種方法是使用這種方式的綁定:
this.onTypeSelect.bind(this, 'type1')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.