我需要在新的屏幕或模式窗口中打开触摸的照片,但我甚至无法使用onPress函数获取它的id。从unsplash.com解析的照片。
我试着用<TouchableWithoutFeedback>和我按下的照片的onPress{}接收id来包装我的视图。
getPhoto函数:
_getPhoto = () => {
alert(this.state.item.id);
}主视图:
<TouchableWithoutFeedback onPress={this._getPhoto}>
<View style={styles.MainContainer}>
<FlatList
data={ this.state.dataSource }
numColumns={2}
ItemSeparatorComponent = {this.FlatListItemSeparator}
renderItem={({item}) =>
<View style={{flex:1, flexDirection: 'column'}}>
<Image source = {{ uri: item.urls.raw }} style={styles.imageView} />
</View>
}
keyExtractor={(item, index) => index.toString()}
extraData={this.state}
/>
</View>
</TouchableWithoutFeedback>我希望至少从json接收到照片id,但大多数情况下我会收到诸如"undefined is not a object“之类的错误。
我这样解决了这个问题:
renderItem={({item}) =>
<TouchableWithoutFeedback onPress = {this._getPhoto.bind(item.id)}>
<View style={{flex:1, flexDirection: 'column'}}>
<Image source = {{ uri: item.urls.raw }} style={styles.imageView} />
</View>
</TouchableWithoutFeedback>
}但现在onPress在应用程序发布方面发挥了作用。
发布于 2019-06-30 23:27:47
答案是在绑定中。我将item.id绑定到get函数:
renderItem={({item}) =>
<TouchableWithoutFeedback onPress = {this._getPhoto.bind(item.id)}>
<View style={{flex:1, flexDirection: 'column'}}>
<Image source = {{ uri: item.urls.raw }} style={styles.imageView}
</View>
</TouchableWithoutFeedback>
}并将_getPhoto的内容更改为:
_getPhoto(){
alert(this);
}所以它的工作方式就是这样。
https://stackoverflow.com/questions/56825324
复制相似问题