useLocalStorage

A hook to manage a value in the local storage.

Signature

function useLocalStorage<T>(key: string, initialValue?: T): {
  value: T | undefined;
  setValue: (value: T) => Promise<void>;
  removeValue: () => Promise<void>;
  isLoading: boolean;
}

Arguments

  • key - The key to use for the value in the local storage.

  • initialValue - The initial value to use if the key doesn't exist in the local storage.

Return

Returns an object with the following properties:

  • value - The value from the local storage or the initial value if the key doesn't exist.

  • setValue - A function to update the value in the local storage.

  • removeValue - A function to remove the value from the local storage.

  • isLoading - A boolean indicating if the value is loading.

Example

Last updated