0
我正在尝试使用TouchableOpacity.onPress
处理ListView
中的一排物品的水龙头,但我尝试的所有内容都以一种稍微不同的方式失败。我有renderRow
函数,提供给ListView.renderRow
。在这里我想做的事:如何使用`TouchableOpacity`中的数据获得'onPress`回调
renderRow(rowData) {
return (
<TouchableOpacity onPress= {() => this.tapRow(rowData) }>
<View style={{margin: 5, backgroundColor: "#EEE" }}>
<Text>{rowData.text}</Text>
</View>
</TouchableOpacity>
)
}
而且我tapRow
功能是:
tapRow(rowData) {
console.log("Tapped Row: " + rowData.id)
}
这将导致错误_this3.tapRow is not a function ... _this3.tapRow is undefined
。
查看参考资料我也尝试将this.tapRow = this.tapRow.bind(this)
添加到构造函数中,但这没有帮助。
对于测试我也做了onPress={this.tapRow}
。这不会导致错误,但函数永远不会被调用(我做一个简单的console.log来验证)
如何获得在onPress
中调用的函数并为其提供参数?
尝试这样做就tapRow,tapRow =(rowData)=> { 的console.log( “TEST”); } –
@GaneshCauda这是我试过的东西,现在再试一次。同样的问题'_this3.tapRow'未定义。 –
ah和onpress,试着做这件事,onPress = {()=> this.tapRow({rowData})} –