2017-09-04 92 views
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中调用的函数并为其提供参数?

+0

尝试这样做就tapRow,tapRow =(rowData)=> { 的console.log( “TEST”); } –

+0

@GaneshCauda这是我试过的东西,现在再试一次。同样的问题'_this3.tapRow'未定义。 –

+0

ah和onpress,试着做这件事,onPress = {()=> this.tapRow({rowData})} –

回答

0

绑定构造的renderRow功能:

constructor (props) { 
    super(props) 
    this.renderRow = this.renderRow.bind(this) 
} 
相关问题