UNPKG

@react-hookz/web

Version:

React hooks done right, for browser and SSR.

47 lines (46 loc) 2.26 kB
import { INextState } from '../util/resolveHookState'; export declare type IUseStorageValueOptions<InitializeWithValue extends boolean | undefined = boolean | undefined> = { /** * Whether to store default value to store. * * @default false */ storeDefaultValue?: boolean; /** * Disable synchronisation with other hook instances with the same key on the same page. * * @default false */ isolated?: boolean; /** * Subscribe to window's `storage` event. * * @default true */ handleStorageEvent?: boolean; } & (InitializeWithValue extends undefined ? { /** * Whether to initialize state with storage value or initialize with `undefined` state. * * Default to false during SSR * * @default true */ initializeWithStorageValue?: InitializeWithValue; } : { initializeWithStorageValue: InitializeWithValue; }); export declare type IReturnState<T, D, O, N = D extends null | undefined ? null | T : T, U = O extends { initializeWithStorageValue: false; } ? undefined | N : N> = U; export declare type IHookReturn<T, D, O> = [ IReturnState<T, D, O>, (val: INextState<T, IReturnState<T, D, O>>) => void, () => void, () => void ]; export declare function useStorageValue<T = unknown>(storage: Storage, key: string, defaultValue?: null, options?: IUseStorageValueOptions): IHookReturn<T, typeof defaultValue, IUseStorageValueOptions<true | undefined>>; export declare function useStorageValue<T = unknown>(storage: Storage, key: string, defaultValue: null, options: IUseStorageValueOptions<false>): IHookReturn<T, typeof defaultValue, typeof options>; export declare function useStorageValue<T>(storage: Storage, key: string, defaultValue: T, options?: IUseStorageValueOptions): IHookReturn<T, typeof defaultValue, IUseStorageValueOptions<true | undefined>>; export declare function useStorageValue<T>(storage: Storage, key: string, defaultValue: T, options: IUseStorageValueOptions<false>): IHookReturn<T, typeof defaultValue, typeof options>; export declare function useStorageValue<T>(storage: Storage, key: string, defaultValue?: T | null, options?: IUseStorageValueOptions): IHookReturn<T, typeof defaultValue, typeof options>;