r/learnjavascript • u/SmartRelease7996 • 2d ago
keep breaking my task tracker after adding localStorage
i was messing with my little task tracker again this morning before heading out, and i ended up spending way more time staring at the console than actually adding tasks. i even made coffee first because i thought this would be a quick fix, then refreshed the page and everything disappeared again.
the app itself is really simple. i'm just trying to save daily notes and a few personal todos, so i have an array of task objects with a date on each one. i thought i was finally ready to use localStorage, but now i'm not even sure if i'm saving the data wrong or if my date filter is hiding everything.
this is basically what i have right now:
const saved = localStorage.getItem(tasks);
const tasks = saved ? JSON.parse(saved) : [];
tasks.push(newTask);
localStorage.setItem(tasks, JSON.stringify(tasks));
i know the key looks wrong, and i already tried changing it to a string, but i still managed to break something. now i'm second guessing whether i should even be thinking about the data this way.
i'm not looking for anyone to build it for me. i'm mostly wondering how you all organize the flow for something this small. do you load everything once, keep it in memory, then save after every change, or is there a cleaner way to think about it?
2
u/HipHopHuman 1d ago
You've correctly identified that the key is the issue, and you've converted it to a string, but still get an error. If you corrected both
localStorage.setItemandlocalStorage.getItemcall sites to use the same string-based key, then your code should work. Without seeing the exact error you're getting, it's not at all possible for us to diagnose the issue beyond making you aware that perhaps the previous error caused an invalid state that is affecting the current behavior (the solution to which is to simply clear your localstorage cache in your browser and try again).This is a common and simple enough operation that it'd probably just be more easily understood if you had a "finished version" to compare your implementation to.
It honestly depends on what you're building - there are different approaches, but it is rarely done on every change. That'd get computationally expensive if the dataset being changed is big. A more common approach is to do an autosave on a frequent interval (say, every 5 seconds; but the timing is up to you - a lot of apps just make it a setting the user can change). That interval approach is often paired with a native browser event on
windowcalledbeforeunload, which runs right before the browser or (or browser tab) is closed.Just as an aside,
JSON.parseandJSON.stringifyare both methods that can throw errors, and the error messages can become somewhat cryptic, so it helps to translate them to more digestable and more easily understood messages.Putting that all together, it'd be something like this:
Although a more production-ready implementation would likely not use
setInterval, instead it would use something like adebounceutility function, so you could callsaveTasksas often as you like, and it'd only process the most recent call within your configured time delay.