Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,199,733 members, 7,972,665 topics. Date: Friday, 11 October 2024 at 03:22 PM |
Nairaland Forum / Science/Technology / Programming / I Need Help With This React Code. (550 Views)
Best Ways To Write Cleaner React Code (2) (3) (4)
I Need Help With This React Code. by Nobody: 9:28pm On Jul 07, 2022 |
I believe the code is fairly explanatory. data.commentsis an array of comments which I’m passing down as the initial value of commentsthrough the commentsArraystate. Now I’m trying to preserve this value commentsin local storage. When I add a comment and check local storage I can see that the array is stored there with all the comments. But whenever I refresh the browser all the comments are gone from the UI I don’t know what I’m screwing up
|
Re: I Need Help With This React Code. by jbreezy: 9:37pm On Jul 07, 2022 |
Your data keeps disappearing because you didn't retrieve the previously stored data in your local storage before storing a new data. You have to first retrieve d stored data in your local storage, spread it with the current data you want to store, then stringify. |
Re: I Need Help With This React Code. by Karleb(m): 9:39pm On Jul 07, 2022 |
jbreezy: Mark this answer as the best answer. |
Re: I Need Help With This React Code. by jbreezy: 9:41pm On Jul 07, 2022 |
Karleb:Thanks. |
Re: I Need Help With This React Code. by Nobody: 10:27pm On Jul 07, 2022 |
jbreezy:Hmm do you mind showing me how? I tried doing it and now it’s throwing exceeded quota error. |
Re: I Need Help With This React Code. by Nobody: 11:09pm On Jul 07, 2022 |
Your problem is visible enough in your last code. You are setting comments again to an empty array or object. on the second line of your code you are setting comments to commentArray. now when your page refresh, at the top comments will be set to commentArray which is the initial and then your useEffect will run again and setItem to comments which is probably empty at this time so many comments comments variables mehn |
Re: I Need Help With This React Code. by Karleb(m): 11:29pm On Jul 07, 2022 |
Rgade: Share the code giving you error comments = [... comments, newComment] Before store to local storage should suffice |
Re: I Need Help With This React Code. by chukwuebuka65(m): 3:17pm On Jul 08, 2022 |
Do this: setComentsArray( prevcomment => [ …prevcomment, Comments ] ). Instead of setCommentsArray( Comments ) Also do this: useEffect( () => { return ( ) => { local storage.setItems(“comments”, comments) }}) . Instead of the way u did it |
Re: I Need Help With This React Code. by chukwuebuka65(m): 3:18pm On Jul 08, 2022 |
Rgade: Read my comment above |
(1) (Reply)
8 AI Tools You Should Use On A Daily Basis / SEO Specialist Needed / .
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 22 |