【问题标题】:Displaying FlatList using array of objects使用对象数组显示 FlatList
【发布时间】:2021-05-18 14:54:02
【问题描述】:

我是 react-native 的新手,我有这些数据,我想在我的 Flatlist 中获取 privateUrl。

console.log(information)

返回:

{
"images": [
    {
      "_id": "60a39eda33c50c377afd9cef",
      "name": "155055995_4635322536484663_1177699315069654339_n.jpg",
      "sizeInBytes": 677009,
      "publicUrl": null,
      "privateUrl": "tenant/607b7b33b168410028b2a095/informations/images/dad8915d-2292-4a9a-be2e-1b2fe711069f.jpg",
      "updatedAt": "2021-05-18T14:07:42.859Z",
      "createdAt": "2021-05-18T14:07:42.859Z",
      "id": "60a39eda33c50c377afd9cef",
      "downloadUrl": "http://172.16.224.151:8080/api/file/download?privateUrl=tenant/607b7b33b168410028b2a095/informations/images/dad8915d-2292-4a9a-be2e-1b2fe711069f.jpg"
    }
  ],
  "email": null,
  "site_web": "https://www.git-tower.com/learn/git/faq/solve-merge-conflicts/",
  "num_Tel": null,
  "type": "news",
  "publish": "true"
}

这是我的平面列表:

     <FlatList    
        data={information}
        windowSize={60} 
        initialNumToRender={6} 
        keyExtractor={(item, index) => index.toString()}
        renderItem={({ item, index }) => (
          <HotelItem
            block
            image={image}
            name={item.titleFR}
            location={item.createdAt}
            style={{ marginLeft: 15, marginBottom: 15 }}
            onPress={() => {
              /* 1. Navigate to the Details route with params */
              navigation.navigate('HotelDetail', {
                infoId: item._id,
              });
            }}

          />
        )}
      />

我想在我的平面列表中获取 privateUrl,我尝试了这个:

 image={item.images.privateUrl}

但不工作。

【问题讨论】:

    标签: reactjs react-native react-native-flatlist


    【解决方案1】:

    images 是一个数组。如果你想引用数组的第一个元素,它应该是item.images[0].privateUrl

    总是只有一张图片吗?我试图思考为什么images 是一个数组而不仅仅是一个对象

    【讨论】:

    • 感谢 Ron B 的快速响应,因为我可以上传更多图片。在这种情况下,我想显示这个数组中的第一个元素。我试图写这样的东西,但我收到错误“无法读取未定义的属性'privateUrl'
    • HI 开发人员,我试过这个并为我工作:image={item.images[0]} 和组件中:${image.privateUrl}}} />
    猜你喜欢
    • 1970-01-01
    • 2019-10-02
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    相关资源
    最近更新 更多