Local Storage
platformProps.utils.localStorage
Access the localStorage utility through the platform props, and call the functions directly. The utility is exposed as platformProps.utils.localStorage
.
Key has to be of type string and each key will be prefixed by the widgetId, which is referenced as namespace in the util. For example a key will be stored as widgetId.key
in localStorage.
Value can be of type boolean, number or string. In future this will be extended to complex types like objects etc.
This API is available only through platformProps.utils
.
Your use of storage is an API
set
function set(key: string, value: string | number | boolean): void;
Example
platformProps.utils.localStorage.set("key1", true);platformProps.utils.localStorage.set("key2", 100);platformProps.utils.localStorage.set("key3", test);
get
function get(key: string): string | boolean | number;
Example
platformProps.utils.localStorage.get("key1");
getAll
function getAll(): Record<string, T>;
getAll will return an object containing all the keys and corresponding values stored in local storage
Example
platformProps.utils.localStorage.getAll();
clear
function clear(): void;
Clear will remove all keys and values from localStorage
Example
platformProps.utils.localStorage.clear();
remove
function remove(key: string): void;
Remove will delete the specific key from localStorage
Example
platformProps.utils.localStorage.remove("key1");
size
function size(): number;
Size will return total number of keys that are stored in localStorage
Example
platformProps.utils.localStorage.size();
keys
function keys(): string[];
This function will return all keys that are in localStorage
Example
platformProps.utils.localStorage.keys();
Setup
To use the localStorage util in any widget, add localStorage util to platformProps.
export type PlatformProps = { utils: { localStorage: { readonly set: (key: string, value: string | number | boolean) => void; readonly get: <T extends string | number | boolean>(key: string) => T; readonly getAll: <T_1 extends string | number | boolean>() => Record< string, T_1 >; readonly clear: () => void; readonly remove: (key: string) => void; readonly size: () => number | undefined; readonly keys: () => string[]; }; };};
Errors
An error will be thrown incase of an exception due to store2 library, in the abscence of widgetId or when the functions are invoked incorrectly.
To learn more about using any of the store2 functions, refer to public documentation here.