Beta

usePersistedState

React hook to sync state with localStorage or sessionStorage.

Made by lucas
Name (localStorage)
Count (persisted)
0

Values survive page refreshes via localStorage.

Installation

Usage

const [theme, setTheme, clear] = usePersistedState("theme", "dark");

// With options
const [value, setValue, clear] = usePersistedState("my-key", defaultValue, {
  storage: "session",
});

Props

PropTypeDefault
deserialize?
(value: string) => T
-
serialize?
(value: T) => string
-
storage?
"local" | "session"
-
defaultValue
T
-
key
string
-