首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用onPress函数获取照片id?

如何使用onPress函数获取照片id?
EN

Stack Overflow用户
提问于 2019-06-30 21:45:06
回答 1查看 60关注 0票数 1

我需要在新的屏幕或模式窗口中打开触摸的照片,但我甚至无法使用onPress函数获取它的id。从unsplash.com解析的照片。

我试着用<TouchableWithoutFeedback>和我按下的照片的onPress{}接收id来包装我的视图。

getPhoto函数:

代码语言:javascript
复制
_getPhoto = () => {
    alert(this.state.item.id);
}

主视图:

代码语言:javascript
复制
<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“之类的错误。

我这样解决了这个问题:

代码语言:javascript
复制
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在应用程序发布方面发挥了作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-30 23:27:47

答案是在绑定中。我将item.id绑定到get函数:

代码语言:javascript
复制
    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的内容更改为:

代码语言:javascript
复制
    _getPhoto(){
    alert(this);
    }

所以它的工作方式就是这样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56825324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档