Local Storage with React Native

Asked

Viewed 1,400 times

0

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

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

As a reference would be something like having a preference file on android or Sqlite database where I would save a lot of basic data that are used as configuration of the application.

I did not understand the application of Assyncstorage because among the data I need to save is an "array".

You could help me with that question?

  • If you need something relational, and you don’t want to use Asyncstorage you can try Realmdb

1 answer

3


You can use Asyncstorage to store all data types locally.

For basic variables you can simply insert in arrows to store

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

to fetch 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 store as follows:

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

to fetch the information just parse back to the object, thus:

var someArrayString = await AsyncStorage.getItem('someArrayName');
var someArray = JSON.parse(someArrayString );

Browser other questions tagged

You are not signed in. Login or sign up in order to post.