Wraps UserDefaults (iOS) and SharedPreferences (Android) into a consistent interface for React Native.
To install prefs
with npm:
npm install @candlefinance/prefs
To install prefs
with yarn:
yarn add @candlefinance/prefs
This package is designed for usage with Effect, but also exposes a Promise-based interface that can be used in any React Native project.
To use prefs
with Effect, check out the example app:
import { getPref, setPref, deletePref } from '@candlefinance/prefs'
To use prefs
without Effect (functions throw a PrefError
on unexpected issues):
import { getPref, setPref, deletePref } from '@candlefinance/prefs/promises'
await setPref('themeColor', 'black')
// string | undefined
const themeColor = await getPref('themeColor')
await deletePref('themeColor')
-
Effect requires that
TextEncoder
/TextDecoder
be defined at runtime. If you don't already have a polyfill, we recommend installing fast-text-encoding using NPM or Yarn and addingimport 'fast-text-encoding'
at the top of your app'sindex.js
file. -
SharedPreferences on Android does not expose a default app-wide instance (like UserDefaults on iOS). To a) access prefs saved using this library from native Android code, or b) write prefs from native Android code and then access them using this library, initialize SharedPreferences in your native Android code with the file name
com.candlefinance.prefs
. -
Both SharedPreferences and UserDefaults support saving various data types including strings, arrays, and numbers. This library only supports saving and retrieving string values. If you attempt to retrieve a non-string value set via native code, this library will return a PrefError with the code
@candlefinance.prefs.non_string_value
.
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT
Made with create-react-native-library