Rehooks
Hooks

useLocalStorage

A hook that manages a state variable in sync with local storage, allowing data persistence across browser sessions.

useLocalStorage

A nifty little hook that keeps your state variable in sync with the local storage, ensuring your data sticks around even when the browser session ends.

Usage

import { useLocalStorage } from "rehooks-ts";
 
const [name, setName] = useLocalStorage("name", "John Doe");

API

useLocalStorage

function useLocalStorage<T>(
  key: string,
  initialValue: T,
): [T, (value: T) => void];

Parameters

NameTypeDescription
keystringThe key for the local storage item.
initialValueTThe initial value for the state variable.

Returns

NameTypeDescription
storedValueTThe current value synchronized with local storage.
setValue(value: T) => voidA function to update the state and sync it with local storage.

On this page