UNPKG

@rehooks/local-storage

Version:
52 lines 1.77 kB
export declare const LOCAL_STORAGE_CHANGE_EVENT_NAME = "onLocalStorageChange"; export interface LocalStorageEventPayload<TValue> { key: string; value: TValue; } /** * Checks if the event that is passed in is the same type as LocalStorageChanged. * * @export * @template TValue * @param {*} evt the object you wish to assert as a onLocalStorageChange event. * @returns {evt is LOCAL_STORAGE_CHANGE_EVENT_NAME} if true, evt is asserted to be onLocalStorageChange. */ export declare function isTypeOfLocalStorageChanged<TValue>(evt: CustomEvent): boolean; /** * Use this instead of directly using localStorage.setItem * in order to correctly send events within the same window. * * @example * ```js * writeStorage('hello', JSON.stringify({ name: 'world' })); * const { name } = JSON.parse(localStorage.getItem('hello')); * ``` * * @export * @param {string} key The key to write to in the localStorage. * @param {string} value The value to write to in the localStorage. */ export declare function writeStorage<TValue>(key: string, value: TValue): void; /** * Use this function to delete a value from localStorage. * * After calling this function, the localStorage value will be null. * * @example * ```js * const user = { name: 'John', email: 'John@fakemail.com' }; * * // Add a user to your localStorage * writeStorage('user', JSON.stringify(user)); * * // This will also trigger an update to the state of your component * deleteFromStorage('user'); * * localStorage.getItem('user') === null // ✔ This is now null * ``` * * @export * @param {string} key The key of the item you wish to delete from localStorage. */ export declare function deleteFromStorage(key: string): void; //# sourceMappingURL=local-storage-events.d.ts.map