Skip to content
Use SharedPreference (Android) and UserDefaults (iOS) with React Native over a unified interface
Java Objective-C Ruby JavaScript
Branch: master
Clone or download
Latest commit 7331a68 Jan 10, 2020

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
android bump version to 1.4.1 Oct 10, 2019
ios Clean up header file Sep 28, 2019
.gitignore Initial commit Dec 8, 2016
LICENSE Closes #11 Aug 16, 2017
README.md Update README.md Sep 28, 2019
RNDefaultPreference.podspec Add support for tvOS Oct 15, 2019
index.d.ts TypeScript typings: getMultiple now return RNDefaultPreferenceKeys Jan 9, 2020
index.js Initial commit Dec 8, 2016
package.json bump version to 1.4.1 Oct 10, 2019

README.md

npm version

react-native-default-preference

Use SharedPreferences (Android) and UserDefaults (iOS) with React Native over a unified interface. All data is stored as a string. If you need to support more complex data structures (e.g. JSON), use serialization/deserialization.

Getting started

$ npm install react-native-default-preference --save

React Native >= 0.60

$ cd ios && pod install

React Native <= 0.59

$ react-native link react-native-default-preference

Manual installation

iOS

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-native-default-preference and add RNDefaultPreference.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNDefaultPreference.a to your project's Build PhasesLink Binary With Libraries
  4. Run your project (Cmd+R)<

Android

  1. Open up android/app/src/main/java/[...]/MainApplication.java
  • Add import com.kevinresol.react_native_default_preference.RNDefaultPreferencePackage; to the imports at the top of the file
  • Add new RNDefaultPreferencePackage() to the list returned by the getPackages() method
  1. Append the following lines to android/settings.gradle:
    include ':react-native-default-preference'
    project(':react-native-default-preference').projectDir = new File(rootProject.projectDir, 	'../node_modules/react-native-default-preference/android')
    
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
      compile project(':react-native-default-preference')
    

Usage

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')});

API

function get(key:String):Promise<String>;
function set(key:String, value:String):Promise<Void>;
function clear(key:String):Promise<Void>;
function getMultiple(keys:Array<String>):Promise<Array<String>>;
function setMultiple(data:Object):Promise<Void>;
function clearMultiple(keys:Array<String>):Promise<Void>;
function getAll():Promise<Object>;
function clearAll():Promise<Void>;

/** Gets and sets the current preferences file name (android) or user default suite name (ios) **/
function getName():Promise<String>;
function setName(name:String):Promise<Void>;

Cordova Native Storage Compatibility

This module is compatible with cordova-plugin-native-storage.

Android

You need to use the same SharedPreference as in the cordova plugin, to do so add the following line:

import { Platform } from 'react-native';
// ...
if (Platform.OS === 'android') DefaultPreference.setName('NativeStorage');

iOS

You don't need to change the name, as cordova-plugin-native-storage uses the default value.

You can’t perform that action at this time.