It does not wait to display When I get data from AsyncStorage
It returns value before catches the value.
I cannot use Async Await on functions. If so it does not compile.
I have searched around the whole day today but I could not find the answer.
How can I solve this simple problem, but complex to me?
React code
import React from 'react';
import { StyleSheet, View, Text, AsyncStorage, TouchableOpacity } from 'react-native';
import { useSelector, useDispatch } from 'react-redux';
import { getUsername } from '../actions';
export default HomeScreen = ({ navigation }) => {
DisplayUsername = () => {
var info = useSelector(state => state.login_info_reducer);
if (Object.entries(info).length === 0 && info.constructor === Object) {
AsyncStorage.getItem('username').then(name => {
info.username = name;
return info;
});
}
else {
return info;
}
}
return (
<View style={styles.container}>
<TouchableOpacity onPress={() => { navigation.navigate('Friends') }}>
<Text>Welcome {DisplayUsername()}</Text>
<Text>Click Here</Text>
</TouchableOpacity>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}
});