我正在使用react-native-grid-view
节点模块列出我的项目,因为我编写了如下代码:
showPlayer(item) {
Alert.alert("Sample", "showPlayer");
}
renderItem(item) {
return <TouchableOpacity style={{alignItems:"center"}} onPress={this.showPlayer.bind(this, item)}>
<ImageLoad
placeholderSource = {require('../images/PlaceHolder.png')}
style={styles.thumbnail}
isShowActivity = {false}
source={{uri: thumbnailObj.value}}
/>
<Text style={styles.gridText}> {item.name}</Text>
</TouchableOpacity>
}
对于上面的代码,我收到此错误:
undefined不是对象(评估'this.showPlayer.bind')
答案 0 :(得分:2)
这与react-native-grid-view没有问题,您需要将此引用传递给renderItem
函数
renderItem(item, that) {
return <TouchableOpacity style={{alignItems:"center"}} onPress={that.showPlayer.bind(this, item)}>
<ImageLoad
placeholderSource = {require('../images/PlaceHolder.png')}
style={styles.thumbnail}
isShowActivity = {false}
source={{uri: thumbnailObj.value}}
/>
<Text style={styles.gridText}> {item.name}</Text>
</TouchableOpacity>
}
render() {
return (
<View> {this.renderItem(item, this)} </View>
)}