My First Dev.to Post — React TODO App with LocalStorage
I was brushing up on my React knowledge and thought of making a small project. I...
For further actions, you may consider blocking this person and/or reporting abuse
You created the extra render cycle yourself, because of the first side effect that you use to set the initial state. You can get rid of that by providing a function to useState to retrieve the initial state from local storage:
joshwcomeau.com/react/persisting-r...
Some general advice: You should really be careful what you use side effects for, using them incorrectly can create very serious bugs and performance issues in your app.
Thanks for the suggestion, will certainly look in the side effects, so that I'll be more careful next time.
Hello dev
Hey
I am up 24hours man
why ??
Pls can I dm you
You can hire me as a mod I will handle things for you just $50 per week
Thanks for the offer, but I just started posting, If I need something like this, I will certainly contact you.
Ok but can I get your telegram or twitter username so it will be more easier sir
You might be able to find a hook on NPM to handle the syncing of storage for you, like @blocdigital/uselocalstorage, which may make your life a little easier.
Here's a quick example of how it would fit in with your example (though I've not tested it).
Thanks for the feedback and snippet, I will certainly look more in the hook and see how can i implement it to make the app better, and after trying maybe write a follow up post too.
The last time I used that hook it needed 3 render cycles to display the actual local storage value... I hope it has improved since then.
I don't think the hook in my specific example has had that issue. Or at least it's not an issue I've ever noticed but I did write it.
Oh btw, local storage actually produces events, so you can attach event listeners to it in a side effect (what they're actually meant for) and update state from there. Dont forget to remove the event listener in the cleanup function of the side effect though.
Here's a codesandbox, this will also work if you change local storage in other tabs:
codesandbox.io/p/devbox/late-sun-q...
I didn’t know about the events associated with localStorage. I’ll definitely explore that more and make sure to handle cleanup properly when adding event listeners in a side effect. Thanks a lot for the sandbox — really helpful!