How to store/save data in react native.Like we use shared-preference in android, is there any solution for react native.I am new to react-nactive.
please add sample example
Storing Data to localStorage With the setItem() Method Note: In order to store data in localStorage , we must first convert it to JSON string using the JSON. stringify() function. And when we want to retrieve it, we will parse the data using JSON. parse() , converting the JSON string back to a JSON object.
On iOS, AsyncStorage is backed by native code that stores small values in a serialized dictionary and larger values in separate files. On Android, AsyncStorage will use either RocksDB or SQLite based on what is available.
You can use React Native AsyncStorage for storing data to local storage of the device.
import { AsyncStorage } from 'react-native'
Use this to save data
AsyncStorage.setItem('key', 'value');
AsyncStorage accepts value as only string, so you may need to use stringify()
before setting the value to AsyncStorage
And to retrieve data use
AsyncStorage.getItem('your key');
Example Code :
const KEY = 'USER_DATA'
let keyValue = { name: yogi }
AsyncStorage.setItem(KEY,keyValue);
AsyncStorage.getItem(KEY).then(asyncStorageRes => {
console.log(JSON.parse(asyncStorageRes))
});
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