#help with localStorage

1 messages · Page 1 of 1 (latest)

fresh jungle
#

Could anyone be so kind and help me with localStorage? I rebuilt the twiiter clone myself and want to implement it but something strange is going on. I can't send the link here because I have it locally because onload is not working on the scrimba browser.

potent lion
#

Could you post a link to a live version of your project on Netlify or GitHub instead? Can't really answer a question without seeing the code, and it's much easier to answer if one can just directly manipulate it. It's easier to do that in the Scrimba editor, but it can also be done in dev tools as a second-best alternative...

fresh jungle
#

My code is not working on scrimba

fresh jungle
#

@potent lion Here is the link: https://scrimba.com/scrim/co335495f9ab6f0dc442fb286?a=37019.0.3.L228_2. I removed the part with the onload function. My idea was to check if something is saved in localStorage and if so render that out. I tried to save the content of main and also the data array itself but every time strange things happened. By that I mean that all likes disappeared if I clicked something else or I wasn't able to like or retweet anymore. I also feel like my code is not very clean. Maybe you can help me out.

Scrimba

Learn to code with interactive screencasts. Our courses and tutorials will teach you React, Vue, Angular, JavaScript, HTML, CSS, and more. Scrimba is the fun and easy way to learn web development.

potent lion
# fresh jungle <@918632048065904651> Here is the link: https://scrimba.com/scrim/co335495f9ab6f...

You don't need to use onLoad to do what you're trying to do with checking if there's something already saved in localStorage. You can just set up a function that gets invoked in your main JS file and have it follow logic like this:

  1. check if the data is already in local storage.

  2. If so, load that as the data to use for rendering.

  3. If not, use the pre-built data by default.

As for what's going wrong when you try to save things, I have no idea without seeing what you've tried. Except for the commented out code on line 6, I don't see anything related to local storage in what you've shared above

fresh jungle
potent lion
fresh jungle
#

One moment

potent lion
#

Alright, I gotta take my dog out. I'll be back in a bit

fresh jungle
#

Is there a way to make the imported array changeable? I think it's read-only

fresh jungle
#

@potent lion
Now I am in the weird situation again: https://scrimba.com/scrim/co335495f9ab6f0dc442fb286?a=37934.0.3.L20_3
I can load the saved data but then no user interaction is possible anymore.

Scrimba

Learn to code with interactive screencasts. Our courses and tutorials will teach you React, Vue, Angular, JavaScript, HTML, CSS, and more. Scrimba is the fun and easy way to learn web development.

potent lion
#

Ah, I see what you mean

#

So if I click a heart, it will save the heart, and then I can undo the heart (or load it from local storage), but then I can't type in the input anymore

fresh jungle
#

Yeah, basically all user interactions like replying and liking aren’t working after reloading

#

I actually have no idea how to solve this

potent lion
#

I think there are going to be more bugs to sort out after this, but one thing that is standing out to me is that you're still using tweetsData in a lot of functions, as opposed to arrayToUse — which means that you're going to end up loading the local storage array initially, but then it's going to be saving changes to the original array instead of the new array. I'm not sure how much mileage you'll get out of changing that, but maybe try starting there?

fresh jungle
#

I will, but I think there will be much more. I appreciate your help.

potent lion
#

Let me know. I'll be back later