So, I am trying to make an app using react-native and i need a list, i am using 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:
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. How should i fix this?, and make it render the list. Is there a better way to do this?
renderItem={({ item, index } => <Card>...</Card>}(itemisWorks[i]already, and there's no need to manually incrementi); this basically works like when you useArray.mapto render a list in React), see the docs: reactnative.dev/docs/flatlist#renderitemsetStateafter readingWorks? Because that's how React works: the app re-renders after a state change. IfWorksstarts out as empty array, the list will never appear unless you callthis.setState({ Works: ... });If you haven't used React before, I'd recommend learning React first before jumping into React native.