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
Saving data to localStorage in React is super easy: const [data, setData] = useState([]); useEffect(() => { localStorage. setItem('dataKey', JSON. stringify(data)); }, [data]);
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With