Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a simple Synchronous Storage option for react native?

Tags:

react-native

I have a use case for synchronous storage for my react native app.

Before app renders a home view, I want to check if there is a session token stored on the local storage and proceed if it is available, otherwise want to render login component instead as the initial view.

Using sync storage will simplify the code.

like image 940
Joon Avatar asked Apr 27 '16 23:04

Joon


People also ask

What can I use instead of async storage React Native?

React Native AsyncStorage is a simple, unencrypted, asynchronous, persistent, storage system which stores the data globally in the app. It store data in the form of a key-value pair. React Native recommended to use abstraction on top of AsyncStorage instead of AsyncStorage directly as it operates globally.

What is Sync storage React Native?

we are going to use sync-storage library. this library is great that uses async storage to save data asynchronously and uses memory to load and save data instantly synchronously, so we save data async to memory and use in app sync, so this is great.

How many types of storage in React Native?

We have seen four methods of persisting data in React Native, each with its own strength. AsyncStorage is great for storing small data which needs to be serialized. You shouldn't use AsyncStorage for handling relational data in any form. If you are interested in storing relational data, look more into SQLite.

Is React Native async storage deprecated?

Deprecated. Use one of the community packages instead. AsyncStorage is an unencrypted, asynchronous, persistent, key-value storage system that is global to the app. It should be used instead of LocalStorage.


3 Answers

For those who are searching for this, I found this npm package that served this need

react-native-sync-localstorage

like image 192
JavaBanana Avatar answered Oct 16 '22 20:10

JavaBanana


I don't think there is a simple synchronous storage option. According to this answer localstorage is not implemented in the core IOS javascript engine. AFAIK the other options such as those used in this localstorage polyfill don't work. That leaves us with needing a react native module which are asynchonous by design. From the docs:

React Native bridge is asynchronous, so the only way to pass a result to JavaScript is by using callbacks or emitting events

So I think Async is the way to go.

like image 25
Ryan Harmuth Avatar answered Oct 16 '22 21:10

Ryan Harmuth


I have a similar situation and found Realm supports synchronous read, which is what I need. Realm is a bit big for single token storage. I use AsyncStorage for everything else. Let me know if you find a simpler solution.

like image 1
Jeff Avatar answered Oct 16 '22 20:10

Jeff