Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

React Native - Using FlatList and Local Storage

$
0
0

I am trying to get my data from local storage (React Native Async Storage) and then push this data into an array (arrayItems) which is used for the flatlist data. My problem is that my data will not load in the flatlist and im not sure what to do. I believe the issue is because im getting the data from the local storage first after the flatlist has been rendered. I have tried using useEffect to get the data before rendering the flatlist but this does not seem to solve the issue.

My Code:

const [allKeys, setGetLocalStorageKeys] = useState([]);let arrayItems= [];const getLocalStorageKeys = async () => { try {  setGetLocalStorageKeys(await AsyncStorage.getAllKeys()); } catch (e) {}};const getItem = async (prop) => {try {  const item= await AsyncStorage.getItem(prop);  return item != null ? JSON.parse(item) : null;  } catch (e) {}};useEffect(() => {try {  getLocalStorageKeys();  for (let i = 0; i < allKeys.length; i++) {    arrayItems.push(getItem(allKeys[i]));  }} catch (e) {}}, []);return (<View><FlatList    data={arrayItems}    renderItem={({ item }) => <Text>{item}</Text>}    keyExtractor={(item) => item}  /></View>

);

Local Storage https://github.com/react-native-async-storage/async-storage


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>