Reading and Writing localStorage in React
Safely access localStorage in React hooks, handle SSR environments, and serialize complex state.
Reading and Writing localStorage in React is a free React Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The localStorage API
localStorage is a built-in browser store with four core methods: setItem(key, value), getItem(key), removeItem(key), and clear(). Data persists across page reloads and browser restarts.
It lives on the window object and is scoped per origin, meaning each domain has its own isolated storage.
It Is Synchronous and Blocking
Every localStorage call runs synchronously on the main thread. Reading or writing large amounts of data can block rendering and make the UI feel janky.
Because of this, keep stored values small and avoid writing on every keystroke or scroll event.
It Stores Strings Only
localStorage can only hold strings. To store an object or array you must serialize it first with JSON.stringify(value) before calling setItem.
If you store a non-string directly, it gets coerced, so an object becomes the useless string [object Object].
Parsing with Error Handling
When reading, you call JSON.parse(localStorage.getItem(key)), but parsing can throw if the stored string is corrupted or was written by another source.
Wrap the parse in a try/catch and fall back to a default value so a bad entry does not crash your component.
Checking Availability
localStorage is not always usable: during server-side rendering window does not exist, and some private browsing modes throw on write. Guard access with a feature check.
A safe test is a try/catch that sets and removes a dummy key, returning false if it throws.
The ~5MB Capacity Limit
Most browsers cap localStorage at roughly five megabytes per origin. Exceeding it throws a QuotaExceededError on write.
This makes localStorage suitable for small data but a poor choice for images, large datasets, or cached API responses.
Appropriate Data
Good candidates are small, non-sensitive pieces of UI and user state: theme preference, sidebar collapsed state, last-used filters, dismissed banners, and unsaved form drafts.
These are values you want to survive a reload but that do not need a server round trip.
Inappropriate Data
Avoid storing secrets like tokens or passwords, because any script on the page can read localStorage, making it vulnerable to XSS. Also avoid large objects and authoritative server state.
Sensitive auth data belongs in httpOnly cookies, and canonical data belongs on the server.
localStorage vs sessionStorage
Both share the same API, but sessionStorage is cleared when the tab closes and is not shared between tabs. localStorage persists indefinitely and is shared across all tabs of the same origin.
Choose sessionStorage for short-lived, tab-specific data and localStorage for lasting preferences.
vs Cookies and IndexedDB
Cookies are small and sent with every HTTP request, which suits auth but wastes bandwidth for general data. IndexedDB is an asynchronous database for large, structured data well beyond the 5MB limit.
Pick cookies for server-read auth, localStorage for small client prefs, and IndexedDB for big or complex data.
A Simple Read/Write Pattern
A minimal helper writes with localStorage.setItem(key, JSON.stringify(value)) and reads with a guarded try/catch around JSON.parse(localStorage.getItem(key)), returning a default on failure.
This small wrapper keeps serialization and error handling in one place.
Quick Check
Test your understanding of localStorage data types.
Recap
You learned the localStorage API, that it is synchronous, string-only, and capped near 5MB, plus how to parse safely and check availability for SSR and private mode.
You also compared it to sessionStorage, cookies, and IndexedDB to choose the right store.
Frequently asked questions
Is the “Reading and Writing localStorage in React” lesson free?
Yes — the full text of “Reading and Writing localStorage in React” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.
What will I learn in “Reading and Writing localStorage in React”?
Safely access localStorage in React hooks, handle SSR environments, and serialize complex state. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start React Academy?
No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Reading and Writing localStorage in React” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this React Academy lesson?
Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Reading and Writing localStorage in React
- Syncing State with localStorage
- Handling Storage Events Across Tabs
- sessionStorage, IndexedDB, and Choosing the Right API