LocalStorage with useState
Unanswered
Michele posted this in #help-forum
MicheleOP
Hi everyone, I need help with LocalStorage with hook useState
26 Replies
Great black wasp
Just use jotai
MicheleOP
Is there some method from scratch?
MicheleOP
Hi, I have an application where you can select favourite accounts. All these accounts are within a back-end and I show them via the .map method. I should be able to keep the favourite accounts saved, even after the page is closed or reloaded. I have tried doing it this way with localStorage, but it does not work for me. Can you help me?
@Michele Hi, I have an application where you can select favourite accounts. All these accounts are within a back-end and I show them via the .map method. I should be able to keep the favourite accounts saved, even after the page is closed or reloaded. I have tried doing it this way with localStorage, but it does not work for me. Can you help me?
what's your mechanism for updating the
like state? and when you check the application tab on the console > local storage, can you see the LIKE_ACCOUNT value?when you call the
handleLike and you log the value of like what is it?looks like it works. what exactly is your issue?
MicheleOP
If I reload the 'LIKE_ACCOUNT' page it returns false
try adding this to your useEffect:
as in
check if a value exists in localstorage first
if (localStorageData !== null) {
setLike(JSON.parse(localStorageData));
}as in
useEffect(() => {
const localStorageData = localStorage.getItem('LIKE_ACCOUNT');
if (localStorageData !== null) {
setLike(JSON.parse(localStorageData));
}
}, []);check if a value exists in localstorage first
MicheleOP
Value exists in localstorage, but if I reload the page, even with the code you wrote, it returns false
@Michele Value exists in localstorage, but if I reload the page, even with the code you wrote, it returns false
hmm. are you getting any errors whatsoever?
try clearing the localstorage and try it again
try clearing the localstorage and try it again
MicheleOP
I do not receive any errors. I tried cleaning and redoing it, but again after the page has reloaded, it gives me false
i think it has something to do with the use client directive in Next 13.
@Michele I do not receive any errors. I tried cleaning and redoing it, but again after the page has reloaded, it gives me false
try updating your useEffect to use async/await like so:
useEffect(() => {
async function likedAccount() {
const localStorageData = await JSON.parse(
localStorage.getItem("LIKE_ACCOUNT") as string
);
if (localStorageData) setLike(localStorageData);
}
likedAccount();
}, []);you can ignore the
as string if you're not using TypescriptMicheleOP
OK now it works, it stays selected for me. But there's another problem, I have several accounts and consequently I can like several accounts. Now if I select more than one, it only keeps one saved for me
then you need to update your state's data structure to an array
i'm guessing each account has a unique identifier, yeah?
MicheleOP
Yeah exactly. Any account has a unique id
@Michele Yeah exactly. Any account has a unique id
played around on Codesandbox. see if this works - https://codesandbox.io/p/github/codesandbox/codesandbox-template-next.js/csb-7l9pz5/draft/adoring-andras?file=/pages/index.tsx:43,42