Reputation: 49
I have a question about AsyncStorage..
console.log(this.state.UserEmail);
AsyncStorage.setItem('email', JSON.stringify(this.state.UserEmail));
the console log logs the user's email perfectly fine, but for some reason when i try to do
AsyncStorage.getItem("email").then((res) => { this.setState({username: res })});
It seems that the AsyncStorage with the item name of "email" is empty.as it does not return anything if i console.log this.state.username.
Thanks in advance,
Mario
Upvotes: 0
Views: 7394
Reputation: 308
AsyncStorage.getItem("email").then((res) => { this.setState({username: res })});
to:
AsyncStorage.getItem("email").then(async (res) => {
const val = await res;
this.setState({username: val })
});
should wait for response data before doing setState
Upvotes: 0
Reputation:
//check it out !
state = {
UserEmail: '[email protected]'
}
async setValue() {
await AsyncStorage.setItem('email', JSON.stringify(this.state.UserEmail));
}
async getValue() {
try {
const value = await AsyncStorage.getItem('email');
if (value !== null) {
// We have data!!
console.log(value);
}
} catch (error) {
// Error retrieving data
}
}
Upvotes: 4