hightable
Version:
A dynamic windowed scrolling table component for react
29 lines (28 loc) • 1.44 kB
TypeScript
import { Dispatch, SetStateAction } from 'react';
type Parse<T> = (value: string) => T;
type Stringify<T> = (value: T) => string;
interface Options<T> {
key?: string;
parse?: Parse<T>;
stringify?: Stringify<T>;
}
/**
* Hook to use a state that is persisted in local storage.
*
* If the key is not defined, it's a normal useState hook. The only difference is that the initial value is always undefined.
*
* If the key is defined, the initial value is loaded from local storage, and the value is persisted in local storage after each change.
*
* If the key changes, the value is updated from local storage. If the new key is undefined, the value does not change.
* Note that the values stored with a previous key are maintained.
* TODO(SL): add a way to delete them?
*
* @param options
* @param [string | undefined] options.key The key to use in local storage. If undefined, the value is not persisted.
* @param [function] options.parse A function to parse the value from local storage. If not provided, JSON.parse is used.
* @param [function] options.stringify A function to stringify the value to local storage. If not provided, JSON.stringify is used.
*
* @returns [T | undefined, Dispatch<SetStateAction<T | undefined>>] The value and the setter.
*/
export declare function useLocalStorageState<T>({ key, parse, stringify }?: Options<T>): [T | undefined, Dispatch<SetStateAction<T | undefined>>];
export {};