26

Just now I started using AsyncStorage. I tried storing input text value like this:

class Sample extends React.Component{ 
  constructor(props){
    super(props);
     this.state = {
      Name:' ',
    };
  }
componentDidMount() {
    AsyncStorage.getItem("Name").then((value) =>{
      this.setState({"Name":value})
    }).done();
handleChange(e){
      AsyncStorage.setItem("Name", e.nativeEvent.text)
       this.setState({
         email: e.nativeEvent.text
       })
    }
  render(){
    return(
       <View>
        <TextInput
               style={styles.textContainer}
               value={this.state.Name} 
               placeholder="Name"
               onChange={this.handleChange.bind(this)}/>
       </View>
   )
}

For this it is working correctly, but i want to add a record into an array instead of changing the record every time. I want to push the record into an array and need to display the total array data in the view, (i.e) I need previously avilable data also.

6
  • So you want to store an array via AsyncStorage? Commented Jan 7, 2016 at 9:31
  • Yes, i want to store an array via AsyncStorage Commented Jan 7, 2016 at 9:34
  • did my answer solves your question? Commented Jan 12, 2016 at 21:44
  • 1
    No i use react-native-store Commented Jan 13, 2016 at 5:09
  • Where's the problem? Commented Jan 13, 2016 at 6:11

3 Answers 3

52

Stringify Array

Use JSON.stringify and JSON.parse to store an array as a value via AsyncStorage.

Store / Stringify

const stringifiedArray = JSON.stringify(somearray)

Restore / Parse

const restoredArray = JSON.parse(stringifiedArray)

Usage with AsyncStorage

 return AsyncStorage.getItem('somekey')
      .then(req => JSON.parse(req))
      .then(json => console.log(json))
      .catch(error => console.log('error!'));

const someArray = [1,2,3,4];
return AsyncStorage.setItem('somekey', JSON.stringify(someArray))
      .then(json => console.log('success!'))
      .catch(error => console.log('error!'));
Sign up to request clarification or add additional context in comments.

1 Comment

how about if it is a long array?
6

for setting

     AsyncStorage.setItem('name', JSON.stringify(your array));

for getting

  AsyncStorage.getItem('name', (error, result) => {
  this.setState({ name: JSON.parse(result) }, function () {   });});

Comments

1

None of the other answers give an applied demonstration of pushing values back to the array (as opposed to completely replacing it). Below is an example:

// You will first want to set an initial array:
AsyncStorage.setItem("myNumbers", [1, 2, 3])

// Now if we want to append other values to it.
// It's important to enclose it in bracket (otherwise you'd get an error trying to modify an object)

const arr = [await AsyncStorage.getItem("myNumbers")]
arr.push(15)
AsyncStorage.setItem('myNumbers', arr)

If you'd like to get the final result:

const modified = await AsyncStorage.getItem("myNumbers")
console.log(modified) //gives us 1, 2, 3, 15

Comments

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.