Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store records in an array using AsyncStorage

Tags:

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.

like image 660
Hussian Shaik Avatar asked Jan 07 '16 09:01

Hussian Shaik


People also ask

How do you store data in AsyncStorage?

Keep in mind that AsyncStorage can only store string values. In order to store object data you need to serialize it first. You can do this by using JSON. stringify() to store it and JSON.

How do you store data in an array in React Native?

To create and use an array in react native, just use square brackets [] it will create an array like the below example.

How do you store a list in AsyncStorage in React Native?

You can use AsyncStorage to store and load data to/from local storage. One thing to note is data MUST be a string, so anything like an object that is not a string needs to be stringified. You can use JSON. stringify(...) to do this.

Where does AsyncStorage store data?

On iOS, AsyncStorage is backed by native code that stores small values in a serialized dictionary and larger values in separate files. On Android, AsyncStorage will use either RocksDB or SQLite based on what is available.


2 Answers

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!')); 
like image 168
purii Avatar answered Sep 19 '22 21:09

purii


for setting

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

for getting

  AsyncStorage.getItem('name', (error, result) => {   this.setState({ name: JSON.parse(result) }, function () {   });}); 
like image 27
James Siva Avatar answered Sep 19 '22 21:09

James Siva