Android's Native key value storage system in React Native
npm install [email protected] --save
npm install react-native-shared-preferences --save
- In
android/settings.gradle
...
include ':react-native-shared-preferences', ':app'
project(':react-native-shared-preferences').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-shared-preferences/android')
- In
android/app/build.gradle
...
dependencies {
/* YOUR DEPENDENCIES HERE */
compile "com.facebook.react:react-native:+"
compile project(":react-native-shared-preferences") // <--- add this
}
- Register Module (in MainApplication.java)
import in.sriraman.sharedpreferences.RNSharedPreferencesReactPackage; // <--- import
public class MainActivity extends ReactActivity {
......
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RNSharedPreferencesReactPackage() // <--- Add this
);
}
......
}
var SharedPreferences = require('react-native-shared-preferences');
SharedPreferences.setName("name");
SharedPreferences.setItem("key","value");
SharedPreferences.getItem("key", function(value){
console.log(value);
});
SharedPreferences.getItems(["key1","key2"], function(values){
console.log(values)
});
SharedPreferences.getAll(function(values){
console.log(values);
});
SharedPreferences.clear();
SharedPreferences.getAllKeys(function(keys){
console.log(keys);
});
SharedPreferences.removeItem("key");