简体   繁体   English

React Native FlatList 组件没有渲染我的数组

[英]React Native FlatList component is not rendering my array

So, I am trying to make an app using react-native and i need a list, i am using FlatList.所以,我正在尝试使用 react-native 制作一个应用程序,我需要一个列表,我正在使用 FlatList。 It seems like the problem is in the saving when i use console.log() (I save and retrieve in different files(screens), could that be the problem?).I am getting my array from async storage:当我使用console.log()时,问题似乎出在保存中(我在不同的文件(屏幕)中保存和检索,这可能是问题吗?)。我从异步存储中获取我的数组:

try {
  if ((await AsyncStorage.getItem("DA")) !== null) {
    DUES = JSON.parse(await AsyncStorage.getItem("DA"));
    Works = JSON.parse(await AsyncStorage.getItem("WA"));
    await AsyncStorage.removeItem("DA");
    await AsyncStorage.removeItem("WA");
  }
  if ((await AsyncStorage.getItem("DUES")) !== null) {
    DUES.push(await AsyncStorage.getItem("DUES"));
    Works.push(await AsyncStorage.getItem("Info"));
    await AsyncStorage.removeItem("DUES");
    await AsyncStorage.removeItem("Info");
  }
  await AsyncStorage.setItem("DA", JSON.stringify(DUES));
  await AsyncStorage.setItem("WA", JSON.stringify(Works));
} catch (err) {
  console.log(err);
}

And i need to display that on the list, but nothing renders.我需要在列表中显示它,但没有任何渲染。 List code:清单代码:

<FlatList>
      data={Works}
      renderItem=
      {() => {
        <Card>
          <Text>{Works[i]}</Text>
          <Text>Due: {DUES[i]}</Text>
        </Card>;
        i++;
      }}
</FlatList>

Save script:保存脚本:

      onPress={async () => {
        try {
          await AsyncStorage.multiSet([
            ["DUES", D],
            ["Info", Title],
          ]);
          console.log("DONE2");
          Alert.alert("Saved!");
        } catch (err) {
          console.log(err);
        }
      }}

It might be because i am missing the "key", but idk.这可能是因为我错过了“钥匙”,但 idk。 How should i fix this?, and make it render the list.我应该如何解决这个问题?,并使其呈现列表。 Is there a better way to do this?有一个更好的方法吗?

That's because you are not returning the elements that you need to print out in your renderItem method of FlatList component.这是因为您没有返回需要在 FlatList 组件的 renderItem 方法中打印出来的元素。 Instead try this:而是试试这个:

<FlatList>
      data={Works}
      renderItem=
      {({ item, index }) => {
        return (
          <Card>
            <Text>{item}</Text>
            <Text>Due: {DUES[index]}</Text>
          </Card>
        );
      }}
</FlatList>

You should do this instead:你应该这样做:

try {
  const [da, work] = await Promise.all([
    AsyncStorage.getItem("DA"),
    AsyncStorage.getItem("WA")
  ]);

  if (da !== null) {
    DUES = JSON.parse(da);
    Works = JSON.parse(work);
  }

  const [dues, info] = await Promise.all([
    AsyncStorage.getItem("DUES"),
    AsyncStorage.getItem("Info")
  ]);

  if (dues !== null) {
    DUES.push(dues);
    Works.push(info);

    await AsyncStorage.removeItem("DUES");
    await AsyncStorage.removeItem("Info");
  }

  await Promise.all([
    AsyncStorage.setItem("DA", JSON.stringify(DUES)),
    AsyncStorage.setItem("WA", JSON.stringify(Works))
  ]);
} catch (err) {
  console.log(err);
}

Then your list:然后你的清单:

<FlatList>
      data={Works}
      renderItem={({item, index}) => {
        return <Card>
          <Text>{item}</Text>
          <Text>Due: {DUES[index]}</Text>
        </Card>;
      }}
</FlatList>

You could also combine dues and work into a single array of object kind of thing, I guess.我猜你也可以将会费和工作组合成一个单一的 object 数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM