Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native AsyncStorage: Accessing Value from Promise Object

When I use AsyncStorage.getItem() to retrieve the value (an email address) of a specified key, it returns a Promise object as indicated in the documentation. The value appears in object like so:

{
  _45: 0
  _54: null
  _65: "[email protected]"
  _81: 1
}

Can I reliably access this value by calling obj._65 or is there another way to accomplish this?

like image 832
Steed-Asprey Avatar asked Sep 23 '16 17:09

Steed-Asprey


People also ask

How do I get data from Promise object in React Native?

To get promise value in React and JavaScript, we can use await . to create the getAnswer function that calls fetch with await to get the response data from the promise returned by fetch . Likewise, we do the same with the json method. And then we call setAns to set the value of ans .

How do I return Async storage value?

Async Storage can only return string data, so in order to use number or object we have to first converts it from string . To get value from asyncstorage, React native asyncstorage provide getItem() method, it will expect storage key and return value .

Is AsyncStorage deprecated?

Deprecated. Use one of the community packages instead. AsyncStorage is an unencrypted, asynchronous, persistent, key-value storage system that is global to the app. It should be used instead of LocalStorage.


Video Answer


1 Answers

AsyncStorage return a promise. you can use .then for get value

exemple:

AsyncStorage.getItem('key').then((keyValue) => {
  console.log(keyValue) //Display key value
  }, (error) => {
  console.log(error) //Display error
});
like image 173
Alexandre Teixeira Avatar answered Oct 01 '22 19:10

Alexandre Teixeira