简体   繁体   English

React Native如何调用onPress事件?

[英]React Native how to call onPress event?

Hello there i am working on a beginner project of mine with React Native and right now i am not able to handle onPress event. 您好,我正在使用React Native处理我的初学者项目,现在我无法处理onPress事件。

Render : 渲染

_renderRow(feed) {
  return (
    <TouchableHighlight onPress={() => this._pressRow()}>
      <View style={styles.container}>
        <Image
          source={{uri: feed.thumbnail}}
          style={styles.thumbnail}/>
        <View style={styles.rightContainer}>
          <Text style={styles.title}>{feed.title}</Text>
        </View>
      </View>
    </TouchableHighlight>
  );
}

And the onPress function onPress功能

_pressRow() {
  console.log('list item pressed')  
}

Listview 列表显示

<ListView
  dataSource={this.state.dataSource}
  renderRow={this._renderRow}
  style={styles.listView}/>

This is what i get as an error. 这是我得到的错误。

undefined is not a function (evaluating _this3._pressRow()) undefined不是一个函数(评估_this3._pressRow())

EDIT 编辑

Thanks to @Cherniv. 感谢@Cherniv。 I forgot to bind this with the renderRow function, changed my code like that and worked as intended: 我忘了用renderRow函数绑定this ,改变我的代码并按预期工作:

<ListView
    dataSource={this.state.dataSource}
    renderRow={this._renderRow.bind(this)}
    style={styles.listView}/>

是不是因为你在方法声明中的pressRow之前忘记了“_”?

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

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