hookagain

hookagain

Hooks

useLocalStorage

A hook that provides a way to store and retrieve data in the browser's local storage.

Installation

npx shadcn@latest add https://hookagain.vercel.app/r/use-local-storage.json

Usage

import { useLocalStorage } from "@/hooks/use-local-storage"
 
function Example() {
  const [value, setValue] = useLocalStorage<string>("my-key", "initial value")
 
  return (
    <div>
      <p>Value: {value}</p>
      <button onClick={() => setValue("new value")}>
        Update Value
      </button>
      <button onClick={() => setValue((prev) => `${prev}!`)}>
        Append !
      </button>
    </div>
  )
}

On this page