Session Storage
platformProps.utils.sessionStorage
Access the SessionStorage utility through the platform props, and call the functions directly. The utility is exposed as platformProps.utils.sessionStorage
.
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 sessionStorage.
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;
This function will set a namespace prefixed key and value in session storage
Example
platformProps.utils.sessionStorage.set("key1", true);platformProps.utils.sessionStorage.set("key2", 100);platformProps.utils.sessionStorage.set("key3", test);
get
function get(key: string): string | boolean | number;
This function will return value associated with the given key.
Example
platformProps.utils.sessionStorage.get("key1");
getAll
function getAll(): Record<string, T>;
getAll will return an object containing all the keys and corresponding values stored in session storage
Example
platformProps.utils.sessionStorage.getAll();
clear
function clear(): void;
Clear will remove all keys and values from sessionStorage
Example
platformProps.utils.sessionStorage.clear();
remove
function remove(key: string): void;
Remove will delete the specific key from sessionStorage
Example
platformProps.utils.sessionStorage.remove("key1");
size
function size(): number;
Size will return total number of keys that are stored in sessionStorage
Example
platformProps.utils.sessionStorage.size();
Setup
To use the sessionStorage util in any widget, add sessionStorage util to platformProps.
export type PlatformProps = { utils: { sessionStorage: { 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.