I want to get items from AsyncStorage and then pass it to my component, but for some reason it pass param first and then finish the getData()
App.js:
import React from 'react';
import Vocabulary from "./Screens/Vocabulary";
import AsyncStorage from '@react-native-async-storage/async-storage'
export default function App() {
var vocabulary = []
const getData = async () => {
try {
const value = await AsyncStorage.getItem('@vocabulary')
vocabulary = value != null ? JSON.parse(value) : [];
console.log(vocabulary)
} catch(e) {}
}
getData()
return <Vocabulary vocabulary={vocabulary} />;
}
VocabularyScreen.js:
import React from 'react';
import { View, SafeAreaView, TouchableOpacity } from 'react-native';
import styles from "./styles";
import { Feather } from '@expo/vector-icons';
import AsyncStorage from '@react-native-async-storage/async-storage';
export default function VocabularyScreen({ vocabulary }) {
console.log(vocabulary)
const Plus = async () => {
vocabulary.push({ word: "", translation: "", description: ``, isMarked: false });
await AsyncStorage.setItem('@vocabulary', JSON.stringify(vocabulary))
}
//SCREEN
return (
<View style={styles.container}>
<TouchableOpacity onPress={Plus}>
<Feather name="plus" size={40} color="#3D5201" />
</TouchableOpacity>
</View>
)
}
Console after 3 times pressing on button and restart:
[] //log from VocabularyScreen.js
(3) [{...}, {...}, {...}] //log from App.js
If you know why this happens and how could I pass the changed param to the component please let me know
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…