react-native-rnpreferencesutils
v1.1.0
Published
A package for working with native shared preferences
Downloads
3
Maintainers
Readme
RNSharedPreferences
A package for working with native shared preferences
Overview
This package provides a lot of function for save and retrieve data from shared preference. By this package save and retrieve string,boolean,float,long and List values.
Installation
< RN 0.47
npm install [email protected] --save
>= RN 0.47
npm install react-native-rnpreferencesutils --save
Project setup and initialization
- In
android/settings.gradle
...
include ':react-native-rnpreferencesutils', ':app'
project(':react-native-rnpreferencesutils').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-rnpreferencesutils/android')
- In
android/app/build.gradle
...
dependencies {
compile "com.facebook.react:react-native:+"
compile project(":react-native-rnpreferencesutils") // <--- add this
}
- Register Module (in MainApplication.java)
import com.rnpreferenceutil.PreferencePackage;; // <--- import
public class MainActivity extends ReactActivity {
......
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new PreferencePackage() // <--- Add this
);
}
......
}
Usage
Import
import RNPrefs from "react-native-rnpreferencesutils";
Save string
RNPrefs.saveString("key","value");
Retrieve string value
RNPrefs.getString('key').then(value => {
console.log(value);
});
Save boolean
RNPrefs.saveBoolean("key","value");
Retrieve boolean value
RNPrefs.getBoolean('key').then(value => {
console.log(value);
});
Save StringSet
RNPrefs.saveStringSet("key","value");
Retrieve StringSet value
RNPrefs.getStringSet('key').then(value => {
console.log(value);
});
. . .
Clear all values
RNPrefs.clearAll();
Remove Item
RNPrefs.remove("key");