UNPKG

@d3vtool/hooks

Version:

Collection of custom React hooks to simplify tasks in your React projects.

52 lines (51 loc) 1.99 kB
import { DeSerializerFn } from "./usePersistentState"; /** * A custom hook that reads the persistent state from `localStorage` without providing the ability to update it across windows/tabs. * The hook supports deserializing the state value from `localStorage` using a custom deserialization function. * * Unsupported types in `localStorage` may require a custom deserialization process. * * @template T - The type of the state value. Defaults to `unknown` if not provided. * * @param {string} key - The key under which the state is stored in `localStorage`. * @param {DeSerializerFn<T>} [deserializerFn=deserialize] - Optional function to deserialize the stored value. Defaults to a generic `deserialize` function. * * @returns {T | undefined} - Returns the deserialized state value if found in storage, or `undefined` if no value is found for the given key. * * @example * // Reading a number value from persistent storage: * const state = useReadPersistentState<number>("counter"); * return ( * <div> * Read: {state} * </div> * ); * * @example * // Reading an object value from persistent storage with a custom deserializer: * * class User { * constructor(name: string) { * this.name = name; * } * * static serializerFn(user: User) { * return user.name; * } * * static deserializerFn(data: string) { * return new User(data); * } * } * * const state = useReadPersistentState<User>('user', User.deserializerFn); * return ( * <div> * User: {state?.name} * </div> * ); * * @note The following types are not supported natively by `localStorage` and may need a custom deserialization function: * - "WeakMap", "WeakSet", "Promise", "Error", "EvalError", "RangeError", "ReferenceError", "SyntaxError", "TypeError", "URIError", "Proxy", "Reflect", "WeakRef" */ export declare function useReadPersistentState<T = unknown>(key: string, deserializerFn?: DeSerializerFn<T>): T | undefined;