Local storage with React Native

0

I'm creating an application that needs to store some data locally. These are simple data such as a "Route" path to an HTTP request, or a field with a "time" that will be used for the application to take action.

Knowing this can you tell me how I can save this data?

As a reference it would be something like having a preference file in android or SQLite database where I would save a lot of basic data that is used as application configuration.

I do not understand the application of AssyncStorage because of the data I need to save is an "array".

Would you help me with this question?

    
asked by anonymous 17.04.2018 / 06:03

1 answer

2

You can use AsyncStorage to store all types of data locally.

For basic variables you can simply insert into the setItem to store

var someText = "abc";
await AsyncStorage.setItem('someTextName', someText);

To get the information you can use getItem

var someText = await AsyncStorage.getItem('someTextName');

For cases where you need to store objects or arrays you can use JSON to save as follows:

var someArray = ["abc", "def", "ghi"];
await AsyncStorage.setItem('someArrayName', JSON.stringify(someArray)); 

To search for information just parse back to the object, like this:

var someArrayString = await AsyncStorage.getItem('someArrayName');
var someArray = JSON.parse(someArrayString );
    
17.04.2018 / 13:30