web-shared-preferences
Version:
An simple class to manage the loacal storage, including React like useString, useJSON and more!
21 lines (20 loc) • 1.13 kB
TypeScript
import { StorageImpl } from "./SharedPreferences";
export declare type Dispatch<A> = (value: A) => void;
export declare type SetPrefAction<P> = P | ((prevPref: P) => P);
export declare type CoreGetter<P> = (key: string, defValue: P) => P;
export declare type CoreSetter<P> = (key: string, value: P) => void;
/**
* Used to build different hooks for the `localStorage` implementation
* @param key To get the value from the local storage
* @param defValue Default value to return if the key does not exist
* @param coreGetter
* @param coreSetter
* @returns
*/
export declare function usePref<T>(key: string, defValue: T, coreGetter: CoreGetter<T>, coreSetter: CoreSetter<T>): [T, Dispatch<SetPrefAction<T>>];
export declare function Dispatcher(storage: StorageImpl): {
useString(key: string, defValue: string): [string, Dispatch<SetPrefAction<string>>];
useBoolean(key: string, defValue: boolean): [boolean, Dispatch<SetPrefAction<boolean>>];
useNumber(key: string, defValue: number): [number, Dispatch<SetPrefAction<number>>];
useJSON<T = any>(key: string, defValue: T): [T, Dispatch<SetPrefAction<T>>];
};