0

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

4

1 回答 1

2

这与 react-native-grid-view 没有问题,您需要将 this 的引用传递给 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>
)}
于 2017-08-28T13:37:43.717 回答