[英]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())
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.