Why localStorage data disappears — four causes and how to check each

You asked an AI: "save the task list so it survives a reload." It added localStorage. You add tasks, hit F5 — empty. Or it was all there yesterday and gone today. Or it's on your laptop but not your phone — well, that one is actually normal.
The annoying part: zero errors in the console. localStorage doesn't complain — it just quietly hands back the wrong thing. Let's go through the causes, most common first.
First, learn to see what's really stored. Open DevTools (F12) → the Application tab → Local Storage on the left → your address. You'll see every key and value. That's your main tool for all four checks.
Cause 1: your code overwrites the data on load
The most common one, especially in React. Typical AI code:
const [tasks, setTasks] = useState([]);
useEffect(() => {
localStorage.setItem("tasks", JSON.stringify(tasks));
}, [tasks]);
On the first render, tasks is an empty array. The effect runs and dutifully saves that empty array. On top of yesterday's tasks. And the code that should read them is either missing or runs after the write.
How to check. Add a task and check Application — it's there. Reload the page and look again right away. If the value turned into [] — that's it.
How to fix. Read the saved value right when you create the state, before the first write:
const [tasks, setTasks] = useState(() => {
const saved = localStorage.getItem("tasks");
return saved ? JSON.parse(saved) : [];
});
The function inside useState runs once at startup. After that, the save effect writes the loaded tasks, not emptiness.
Cause 2: a different address means a different storage
Every site has its own storage. And "site" here means scheme, domain and port together. So localhost:3000, localhost:5173 and 127.0.0.1:3000 are three separate storages.
How it happens: yesterday the project started on one port, today that port is busy — and the dev server picked the next one. The data didn't go anywhere; you're just looking in a different drawer. More on this in the localhost and ports explainers.
How to check. Look at the address bar. Application sometimes lists several addresses on the left — check each one.
How to fix. Pin the port in your dev server config. And remember: a preview deploy and the live site are different addresses too. Data won't move from one to the other.
Cause 3: the object was saved as "[object Object]"
localStorage stores only strings. Pass an object directly and it turns into the useless string "[object Object]". You can't read it back.
Numbers are subtler. You saved 5, you read "5". Then "5" + 1 gives "51", not 6. The counter "breaks" even though the data is there.
How to check. Look at the value in Application. See [object Object] — found it.
How to fix. Save with JSON.stringify, read with JSON.parse. More on the format in the JSON explainer. Convert numbers back after reading: Number(value).
Cause 4: the browser cleared it on its own
This happens too, and it's not a bug in your code:
- Incognito mode. All storage is wiped when you close the window.
- Safari. Its anti-tracking protection (ITP) deletes script-written data if the user used Safari for 7 days without visiting your site once.
- The user cleared their history, or the browser freed space because the disk is nearly full.
How to fix. Accept it: localStorage is an on-device cache, not reliable storage. Keep anything important in a database on the server. And while we're at it — data from one device never shows up on another.
localStorage isn't saving data, fix itIn a React component the tasks list is saved to localStorage via useEffect, but after a page reload the list is empty. Check whether the save effect overwrites the data with an empty array on the first render. Rewrite it so tasks is read from localStorage in a lazy useState initializer, with JSON.parse and an empty array fallback. Show the full component.How much data fits in localStorage?
Usually about 5 MB per site. Plenty for settings, drafts and task lists. Not for photos or big files.
What if I use Next.js and get "localStorage is not defined"?
Next.js first renders the page on the server, where there's no browser and no localStorage. The lazy initializer from cause 1 runs there too — and crashes. In Next.js, read storage inside useEffect and keep a "loaded" flag next to it. Let the save effect write only once the flag is set. Then the empty array can't overwrite your data.
Can I store a password or token in localStorage?
Better not. Any script on the page can read it. For sign-in, a cookie with the HttpOnly flag is safer — scripts can't reach it.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





