【问题标题】:React Native: RenderItem in FlatList. How to map array stored in local state?React Native:FlatList 中的 RenderItem。如何映射存储在本地状态的数组?
【发布时间】:2020-09-18 05:24:25
【问题描述】:

我正在使用 React Native 创建一个小型应用程序,该应用程序具有用于存储图像的本地 SQLite 数据库,但在渲染我的图像数组时遇到了问题(从本地数据库获取并存储在本地状态中)。

在我通过映射数据进行渲染之前,效果很好。

  state = {
    items:[when the user uses the app this array fills with images],
  };
<ScrollView>
        {items.map(({ id, value }) => (
          <TouchableOpacity onPress={this.deletePhoto}
            key={id}>
            <Image source={{ uri: value }} style={{ width: null, height: 400 }} />
          </TouchableOpacity>

        ))}
      </ScrollView>
    );
  }

但现在我想更进一步,使用我选择的格式(网格)在 FlatList 中呈现数据。虽然我可以让 FlatList 呈现数组中的图像数量,但我无法让实际图像显示。 不知道如何成功传递数据?

  renderItem = ({ id, value }) => {
  const { items } = this.state;
  if (items.empty === true) {
    return <View style={[styles.item, styles.itemInvisible]} />;
  }
  return (
    <TouchableOpacity style={styles.item} onPress={this.deletePhoto} key={id}>
            <Image source={{ uri: value }} style={{ width: null, height: 400 }} />
    </TouchableOpacity>
  );
  };

对于上下文,这是使用“id”和“value”属性创建“items”SQL 表:

componentDidMount() {
    db.transaction(tx => {
      tx.executeSql(
        'create table if not exists items (id integer primary key not null, value text);'
      );
    });
  }

我想问题是,我如何将 items 数组的属性传递/访问到功能组件中?

更新:

https://ibb.co/hMY1qBy(我得到的 - 例如 DB 条目创建视图但没有图像渲染) https://ibb.co/RN4rqyK(我从下面的答案中得到了什么)

【问题讨论】:

    标签: reactjs react-native


    【解决方案1】:

    从您共享的存储库中获取可重现的代码后,我设法呈现列表中的图像。

    这是你的renderItem 函数

    renderItem = ({ item }) => {
      const { items } = this.state;
      if (items.empty === true) {
        return <View style={[styles.item, styles.itemInvisible]} />;
      }
      return (
        <TouchableOpacity style={styles.item} onPress={this.deletePhoto} key={item.id}>
                <Image source={{ uri: item.value }} style={{ width: 400, height: 120 }} />
        </TouchableOpacity>
      );
      };
    

    您必须从renderItem 函数的参数中获取item,还必须将宽度传递给图像以进行渲染,上面的代码工作正常。

    【讨论】:

    • 太棒了!我明白其中的逻辑。 FlatList 的外观如何?因为我仍然没有得到图像的渲染 - 所以我认为那里遗漏了一些东西。
    • 我已经向您展示了上面的代码和列表,如果仍然无法正常工作,那么我可以将您的代码库中的代码推送到单独的分支中。
    • 得到了他们' - 谢谢 Waheed。
    • 是的,它几乎可以完全工作,只是没有渲染空块(renderItem 函数的顶部。所以弄清楚
    • @JamesStirrat 好的,我明天再调查一下。
    【解决方案2】:

    flatlist renderItem 属性获取一个具有多个属性的参数,主要是 item 和 index,它们通常用于访问行中的项目,您可以使用如下所示的 item

    <FlatList
            data={items}
            renderItem={({ item }) => (
              <TouchableOpacity style={styles.item} onPress={this.deletePhoto} key={item.id}>
                <Image source={{ uri: item.value }} style={{ width: null, height: 400 }} />
        </TouchableOpacity>
            )}
            keyExtractor={item => item.id}
          />
    

    【讨论】:

    • 很遗憾无法正常工作。为 item.id 和 item.value 抛出“未定义不是对象”错误。不知道为什么我不能通过点符号访问这些属性,但可以通过 map 方法传递它们。
    • 返回没有图像的渲染。我将更新我的问题以上传屏幕截图。
    • 看来问题是点表示法在使用map方法调用数组(项目)时还没有调用属性,我们可以通过将属性作为参数传入来调用属性。奇怪
    • 我错过了你代码的空白部分,最好试试这个
    • @JamesStirrat 你能在世博会上做点心吗,我可以给你修好。
    猜你喜欢
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 2022-08-17
    相关资源
    最近更新 更多