10LOC

#local-storage

Reactintermediate

A tiny useLocalStorage hook

import { useState, useEffect } from "react";

export const useLocalStorage = <T>(key: string, initialValue: T) => {
  const [value, setValue] = useState<T>(() => {
    const stored = localStorage.getItem(key);

Sync a single piece of state to localStorage with a small, focused hook.