I add the setItem function of AsyncStorage in the constructor of my Component and then I made a function to fetch it's value using the getItem function, but it returns empty. Am I missing something?
Here's my code:
constructor(props) {
super(props);
try {
AsyncStorage.setItem('testKey', 'I like to save it.');
} catch (error) {
alert("error", error);
// Error saving data
}
}
getData(){
AsyncStorage.getItem('testKey').then((data) => {
alert("data", data);
})
}
Basically, I want to store an accessToken to AsyncStorage, and once it's set, I want to call the getItem function to use it.
Also, the then() function for the setItem is not working:
AsyncStorage.setItem('testKey','blah blah').then(() => {
console.log("I never get executed."); // -> this code never executes for some reason
});
I've been trying to resolve it, but to no avail.
getData()?setItemis asynchronous so depending on when you callgetData(),setItemmay not have finished executing. re: 2nd issue - needs more code. By itself, it's literally impossible for someone to reproduce the error as just that line of code works for me. - Michael Cheng