0
Challenge · 57Routing & Data

Local Storage

Coreused in ~85% of apps

Refresh the page and every useState resets — the half-typed draft, the chosen theme, the dismissed banner all vanish. localStorage is the browser's tiny key/value box on the user's disk that survives reloads, tab closes, and even a reboot.

Core idea. localStorage is a synchronous, per-origin key/value store of strings that persists across sessions; you write with setItem(key, value), read with getItem(key), and use JSON.stringify / JSON.parse to round-trip anything that isn't already a string.
Playground, this isn't a screenshot
pulse.app — draft notepad
const [draft, setDraft] = useState(read)load #1
REACT STATE (RAM)
"Shipping Pulse v2 tonight"
lost on reload
JSON.stringify
localStorage (DISK)
"pulse:draft" : {"text":"Shipping Pulse v2 tonight","words":4}
survives reload & new tabs

Every keystroke is serialized with JSON.stringify and written to disk. Hit Reload page — the in-memory state is wiped, then the lazy initializer reads it back with JSON.parse. The draft survives.

↑ play a little first, then dig in