DEV Community

Discussion on: Storing State on Local Storage in a React Application

Collapse
 
alexanderarce profile image
Alexander Arce • Edited

Why not add the name variable into the useEffect array like so:

useEffect(() => {
    localStorage.setItem('name-state', JSON.stringify(name));
}, [name]);
Enter fullscreen mode Exit fullscreen mode

Then you would only localStorage.setItem if the name is updated, otherwise, if you had another field (surname for example) everytime you update it the localStorage name would be updated.

Collapse
 
uguremirmustafa profile image
uguremirmustafa

That's a good point. As I store just one item, I didn't mind it. I will update it, thanks for the feedback👍