How to check if a key exists in AsyncStorage in React Native? getItem() always returns a promise object
You need to add async await, or add .then to the result
async checkUserSignedIn(){
let context = this;
try {
let value = await AsyncStorage.getItem('user');
if (value != null){
// do something
}
else {
// do something else
}
} catch (error) {
// Error retrieving data
}
}
Like the name says, it's async. So you have to:
AsyncStorage.getItem('user')
.then((item) => {
if (item) {
// do the damage
}
});
If needed, you can play either with local state or some application state management library.
AsyncStorage is async... so, you need to call it this way:
checkUserSignedIn(callback){
AsyncStorage.getItem('user', (err, result) => {
if (!err && result != null){
// do something
}
else {
// do something else
}
callback(result);
});
}