Home > Mobile >  How to save data in react-native
How to save data in react-native

Time:07-05

I want to save data from a page of my react native project. From what I've searched, I must use :

import DefaultPreference from 'react-native-default-preference';
...
DefaultPreference.get('my key').then(function(value) {console.log(value)});
DefaultPreference.set('my key', 'my value').then(function() {console.log('done')});

if I want to save them locally . Howether when I use this inside my code, I got a blank page. What is the issue here? I didn't change anything in my original code apart from thes 3 linnes.

CodePudding user response:

Using "AsyncStorage" is a good choice but I recommended you to use Redux Persist https://github.com/rt2zz/redux-persist

CodePudding user response:

You can take this lib as more popular.

For example code:

import AsyncStorage from '@react-native-async-storage/async-storage';

await AsyncStorage.setItem(key, value);

const value = await AsyncStorage.getItem(key);
console.log('MY_VALUE===>', value);

await AsyncStorage.removeItem(key);

I hope, it is help you.

  • Related