1

I am trying to store values in AsyncStorage as array, but values are not saving there and getting nothing when i try to get the values from array.Any help would be appreciated.Thank you in advance,here is my code :

let searchString = this.state.inputValue

AsyncStorage.getItem('searches', (res) => {
  var searches
  if (res === null) {
    searches = []
  }else {
    searches = JSON.parse(res)
  }
  searches.push({
      searchString: searchString
  })
  AsyncStorage.setItem('searches', JSON.stringify(searches), (res) => {
      console.log('====FirstPage====setItem==async==='+res)
  })
}); 

//getting values from asyncstorage :

AsyncStorage.getItem('searches', (res) => {console.log('===res==='+res)})
3
  • Could you show the code for saving/storing values in AsyncStorage ? Commented Jan 31, 2018 at 5:09
  • Did you use AsyncStorage.getItem() ? Commented Jan 31, 2018 at 5:12
  • I have updated my question. Commented Jan 31, 2018 at 5:20

2 Answers 2

2

Please try this

AsyncStorage.getItem("searches").then((value) => {
    this.setState({"searches": value});
}).done();

Also see https://www.thepolyglotdeveloper.com/2015/09/saving-data-in-your-react-native-mobile-application/

Sign up to request clarification or add additional context in comments.

Comments

0

You can store the data as following:

AsyncStorage.setItem('searches', JSON.stringify(searchString));

In order to fetch/get, you can do that using following:

AsyncStorage.getItem('searches', (err, result) => { // err indicating the error
  console.log("Storage Data(String):", result); // print is string format
  console.log("Storage Data(Object):", JSON.parse(result)); // print is json format
})

2 Comments

input value is saving in asyncstorage now but on second input it overrides the first value not appending the value like an array.
You mean u are doing AsyncStorage.setItem for 'searches' multiple times ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.