@ccm19/shared-pref-module

v0.1.1

Published

React Native Integration of Androids Shared Preferences

Downloads

7

Readme

Shared Preferences Native Module for React-Native

About

CCm19-RN-SharedPrefNativeMod #SharedPrefModule

SharedPrefModule is a native module for React Native that provides access to Android's SharedPreferences system.

It allows you to store, retrieve, and listen for changes to simple key-value pairs persistently, enabling seamless integration with the Android platform.

Features

  • Get and set string and integer preferences.
  • Remove preferences by key.
  • Listen for changes to specific shared preferences.
  • Easily integrate with React Native's event emitter for change notifications.

Install

The installation is carried out using common package managers such as npm or yarn.

Methods

getString(key: String, promise: Promise)

Retrieves a stored string from the shared preferences using the provided key.

  • Parameters:
    • key (String): The key for the preference you want to retrieve.
    • promise (Promise): Resolves with the retrieved string or null if no value is found.

Usage:

SharedPrefModule.getString("user_token").then((value) => {
    console.log("Retrieved value:", value);
});

getInt(key: String, promise: Promise)

Retrieves a stored integer from the shared preferences using the provided key.

  • Parameters:
    • key (String): The key for the preference you want to retrieve.
    • promise (Promise): Resolves with the retrieved integer or -1 if no value is found.

Usage:

SharedPrefModule.getInt("login_count").then((value) => {
    console.log("Retrieved value:", value);
});

initialise(key: String, value: String)

Stores a string value under the specified key in the shared preferences. This is useful for setting initial values.

  • Parameters:
    • key (String): The key under which the value should be stored.
    • value (String): The value to store.

Usage:

SharedPrefModule.initialise("user_token", "abcd1234");

removeString(key: String)

Removes a specific string value from the shared preferences using the provided key.

  • Parameters:
    • key (String): The key for the preference to be removed.

Usage:

SharedPrefModule.removeString("user_token");

setPreference(key: String, value: String)

Stores a string value globally under the specified key in shared preferences. This uses the default shared preferences.

  • Parameters:
    • key (String): The key under which the value should be stored.
    • value (String): The value to store.
SharedPrefModule.setPreference("theme", "dark");

Event Handling

addListener(eventName: String)

Registers a listener for a shared preference change event. Required by React Native's event emitter system.

removeListeners(count: Integer)

Removes the specified number of listeners. Required by React Native's event emitter system.

Note: The module listens for changes to a specific key ("your_wanted_key" in this case). If the value associated with the key changes, an event (onSharedPreferenceChanged) is emitted.

Usage:

import { DeviceEventEmitter } from 'react-native';
DeviceEventEmitter.addListener('onSharedPreferenceChanged', (data) => {
    console.log('Preference changed:', data);
});

Internal Methods

sendEvent(eventName: String, params: WritableMap)

An internal function to send events from the native Android module to the JavaScript side using React Native's DeviceEventEmitter. This is used for emitting preference change events.

[!NOTE] Useful information that users should know, even when skimming content.

[!TIP] Helpful advice for doing things better or more easily.

[!IMPORTANT] Key information users need to know to achieve their goal.

[!WARNING] Urgent info that needs immediate user attention to avoid problems.

[!CAUTION] Advises about risks or negative outcomes of certain actions.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library