简体   繁体   中英

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.

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

_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())

EDIT

Thanks to @Cherniv. I forgot to bind this with the renderRow function, changed my code like that and worked as intended:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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