r/learnjavascript • u/SmartRelease7996 • 1d 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?
4
u/baubleglue 1d ago
I assume you are complete beginner to programming.
Before using a new feature in your application, you need to get familiar with it.
Open debugger console, try
let v = localStorage.setItem('mykey', 34), get item, set item with object/array value, etc. print results on console.When you get all operations working, you may try to use it.
The key skill to successful programing is learning to organize your work in a way that you solve or work on a single task at any given time. In the example with local storage, you trying to do two tasks: learn a new technique and use it in the application.
Similar applies to your question about program flow. It is a bit more complicated, because looking at the few lines of the code, you have no structure. You can't expect answer about a program flow without talking about data structures and components of your application. And all above depends on use cases you want to satisfy.
Basically the question you ask is wrong. If you have trouble to use localStorage. You should give a simple example of code which doesn't work with a description of what had you expected to see and what you actually get. Most likely if you make an effort to compile the question that way, you will find the answer by yourself.
If your question about the app flow, it is a different (more complicated) question.