Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to store/save data in react native

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

like image 777
yogeshwar nair Avatar asked Mar 22 '17 08:03

yogeshwar nair


People also ask

How do I store data in local storage in React Native?

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.

Where does AsyncStorage save data React Native?

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.


1 Answers

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))
});
like image 71
Hariks Avatar answered Sep 18 '22 20:09

Hariks