UNPKG

@athrok/react-native-mmkv

Version:

React Native MMKV persistence wrapper for Athrok state management library.

91 lines (88 loc) 2.2 kB
// react-storage/index.tsx import React from "react"; import { StorageManager } from "athrok"; // react-storage/utils/windowStorage.ts var windowStorageParser = (storage) => ({ /** * Retrieves all keys from the storage. * @returns {string[]} - An array of keys present in the storage. * @example * ```ts * const keys = localStorage.getKeys(); * console.log('Keys:', keys); * ``` */ getKeys: () => { return Object.keys(storage); }, /** * Retrieves the value associated with the given name from the storage. * @param name - The name of the item to retrieve. * @returns - The value associated with the name, or null if the name does not exist in the storage. * @example * ```ts * const value = localStorage.getItem('key'); * console.log('Retrieved value:', value); * ``` */ getItem: (name) => { return storage.getItem(name); }, /** * Sets the value associated with the given name in the storage. * @param name - The name of the item to set. * @param value - The value to set. * @example * ```ts * localStorage.setItem('key', 'value'); * ``` */ setItem: (name, value) => { storage.setItem(name, value); }, /** * Removes the item associated with the given name from the storage. * @param name - The name of the item to remove. * @example * ```ts * localStorage.removeItem('key'); * ``` */ removeItem: (name) => { storage.removeItem(name); } }); var localStorageParser = () => { return windowStorageParser(localStorage); }; var sessionStorageParser = () => { return windowStorageParser(sessionStorage); }; // react-storage/index.tsx var LocalStoragePersistenceProvider = ({ children }) => { React.useState( StorageManager.initialize({ storage: localStorageParser() }) ); return children; }; var SessionStoragePersistenceProvider = ({ children }) => { React.useState( StorageManager.initialize({ storage: sessionStorageParser() }) ); return children; }; var PersistenceProvider = { LocalStorage: LocalStoragePersistenceProvider, SessionStorage: SessionStoragePersistenceProvider }; export { PersistenceProvider };