Command palette

@yasinsensoy/native

React Native · APIs and utilities

Storage

Key-value store.

11 exports

Import

import {
  Storage,
  createStorage,
  storage,
  deleteStorage,
  useStorage,
} from "@yasinsensoy/native";

Storage

class

Key-value store. Reads and writes are synchronous: values are kept in memory and writes are appended to disk in the background. See createStorage.

Opens a store (a second call with the same name returns the same object).

createStorage(options?: StorageOptions): Storage

Examples

const cache = createStorage({ id: "cache" });
const secrets = createStorage({ id: "auth", encrypted: true });

storage

constant

The default store (createStorage()); opened on first use.

storage: Storage

Deletes the store and its file (sign out).

deleteStorage(options?: StorageOptions): Promise<void>

useStorage

function

Uses a value in the store like state: the component re-renders when it changes (even if written elsewhere), and writes persist.

useStorage(key: string, defaultValue: V, target?: Storage): [
    Widen<V>,
    (next: Widen<V> | undefined | ((previous: Widen<V>) => Widen<V> | undefined)) => void
]

Examples

const [onboarded, setOnboarded] = useStorage("onboarded", false);
const [filters, setFilters] = useStorage("filters", { sort: "new" }, cacheStorage);

A value that can be stored as JSON.

type StorageJson = string | number | boolean | null | readonly StorageJson[] | { readonly [key: string]: StorageJson | undefined; }

A value that can be stored: string, number, boolean, null or a JSON object/array.

type StorageValue = StorageJson

createStorage options.

NameTypeDefaultDescription
id?string"default"Store name; stores opened with the same name are the same. Letters, digits, ., -, _.
encrypted?booleanfalseEncrypted store: records are encrypted with AES-256-GCM; the key is kept in the Android Keystore / iOS Keychain and never leaves the device. Read/write speed is almost the same. Encrypted and unencrypted stores with the same name are separate files.

Storage.set options.

NameTypeDescription
ttl?numberDuration (ms): afterwards the value behaves as missing and is deleted (for caches).

Change listener; key is null when the store was cleared.

type StorageListener = (key: string | null) => void

Same interface as AsyncStorage (for libraries that expect it).

NameTypeDescription
getItem(key: string): Promise<string | null>Reads a value; null if missing.
setItem(key: string, value: string): Promise<void>Writes a value.
removeItem(key: string): Promise<void>Removes a value.
mergeItem(key: string, value: string): Promise<void>Merges a JSON value into the existing one.
getAllKeys(): Promise<string[]>All keys.
clear(): Promise<void>Removes everything.
multiGet(keys: readonly string[]): Promise<[string, string | null][]>Reads several values at once.
multiSet(pairs: readonly (readonly [string, string])[]): Promise<void>Writes several values at once.
multiRemove(keys: readonly string[]): Promise<void>Removes several values at once.