在“react-native-grid-view”中获取TouchableOpacity的问题

时间:2017-08-28 12:23:35

标签: gridview react-native react-native-android touchableopacity

我正在使用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')

1 个答案:

答案 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>
)}