Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store value in LocalStorage in React Native

Tags:

react-native

I am new at React Native and I am creating an app in which I need to store values in local storage like SharedPref in Android .

Ex: If user loggedIn then control should go to Dashboard otherwise login screen should appear

Thank You in advance

like image 393
J.sparrow Avatar asked Nov 27 '17 11:11

J.sparrow


People also ask

How do I store values in localStorage in react?

Saving data to localStorage in React is super easy: const [data, setData] = useState([]); useEffect(() => { localStorage. setItem('dataKey', JSON. stringify(data)); }, [data]);

How do you save values in React Native?

To store value in local storage in React Native, we can use AsyncStorage. to call AsyncStorage. setItem with the key and value to storage the entry with the key and value. Then we call AsyncStorage.

Can we use local storage in React Native?

Introduction to React Native Local StorageIt also supports local storage for this purpose. We should not confuse the data stored with the state data as it is not a replacement for that. State Data always gets erased once the app is closed. Local Storage can also be achieved by Async Storage.


1 Answers

There are several options to do such thing.

For most basic things you can use AsyncStorage from react-native, but there are other options, like Realm and SQLite that are libraries to store data on device.

If you are using Redux, you can also use redux-persist.

EDIT

As @honor said, you should use react-native-community/react-native-async-storage instead of the older AsyncStorage referenced here (deprecated)

like image 162
mateusppereira Avatar answered Sep 25 '22 14:09

mateusppereira