【问题标题】:How to show an array of images in react native?如何在本机反应中显示一组图像?
【发布时间】:2019-08-20 17:36:03
【问题描述】:

如何在 react native 中显示图像数组?图像使用 URL 并存储在 firestore 数据库中这是它存储在数据库中的确切结构 lodash 用于将用户对象更改为数组 [![数据库结构][1]][1]

Object {
"book": "Robin hood", 
"photos": Array [ "https://picsum.photos/id/1001/5616/3744", "https://picsum.photos/id/1002/4312/2868", "https://picsum.photos/id/1003/1181/1772", ],
 }

我尝试了这个,但得到错误提示“更新由 RCTImageView 管理的视图的属性“src”时出错 空值 uri 的值不能从 ReadableVativeArray 转换为 String''

return this.props.user.map((details) => {
      return (

        <View >

        <Text>{details.book}</Text>

          <Image style={{ width: 350, height: 300 }} source={{ uri: details.photos }} />

        </View>

        );

    });

我也试过了,控制台日志显示了图像 URL 和其他获取的数据,并且显示了图书详细信息,但由于某种原因,图像没有显示在屏幕上

 return (
      <View style={{ flex: 1 }}>

        {this.props.user.map(details => {
          details.photos.map((image) => {
            console.log(image)
            return (
              <View>
                <Image style={{ width: 350, height: 300 }} source={{ uri: image }} />
              </View>
            );
          })
          return (
            <View>
                  <Text style={styles.b}>{details.book}</Text>
            </View>
          )
        })}
      </View>
    )


  [1]: https://i.stack.imgur.com/dLvci.png

【问题讨论】:

  • 您正在映射图像,但没有对它们做任何事情。他们正在消失在 JS 领域。
  • User 是一个对象,但您正在使用地图对其进行迭代?

标签: reactjs firebase react-native google-cloud-firestore


【解决方案1】:

第二个示例中details.photos.map 的结果未包含在返回值中。相反,你想要这样的东西:

 return (
      <View style={{ flex: 1 }}>

        {this.props.user.map(details => {
          // save the result of the map call to a variable
          const photos = details.photos.map((image) => {
            console.log(image)
            return (
              <View key={image}>
                <Image style={{ width: 350, height: 300 }} source={{ uri: image }} />
              </View>
            );
          })
          return (
            <View>
                  // note the addition of the photos here
                  {photos}
                  <Text style={styles.b}>{details.book}</Text>
            </View>
          )
        })}
      </View>
    )

【讨论】:

    【解决方案2】:

    假设“对象”指的是“this.props.user” 所以,你尝试迭代一个对象但你不能,你只能迭代一个数组,比如 photos 数组。

    试试这个:

    return (
      <View style={{ flex: 1 }}>
    
        <Text>{this.props.user.book}</Text>
    
        {this.props.user.photos.map((photo) => {
          return (
            <Image style={{ width: 350, height: 300 }} source={{ uri: photo }} />
          );
        })}
    
      </View>
    );
    

    【讨论】:

    • 嗨@RoiDayan 我试过了,它抛出了这个错误''类型错误:未定义不是一个对象(评估'this.props.user.photos.map')''
    • 我假设“this.props.user”返回上面的对象。哪个变量保存对象?
    • 嗨@RoiDayan 对象正在转换为带有lodash的数组,该数组显示书名的显示方式而不是照片的显示方式,用户是变量“this.props.user”。
    猜你喜欢
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多