Of course you can use those methods on the localStorage object, but then how do you know when to call them so that what's displayed on the screen is in-sync with what's stored?
One can manually attach the onLocalStorageChange event listeners when each component mounts and clean up when they unmount to avoid memory leak. You see this can get tedious, which is why useLocalStorage exists to help: github.com/rehooks/local-storage/b...
I put together a tiny react app here: stackblitz.com/edit/react-5ldn5l. Hopefully, this can highlight some of the features that op left out from the original snippet.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Of course you can use those methods on the
localStorage
object, but then how do you know when to call them so that what's displayed on the screen is in-sync with what's stored?One can manually attach the
onLocalStorageChange
event listeners when each component mounts and clean up when they unmount to avoid memory leak. You see this can get tedious, which is whyuseLocalStorage
exists to help: github.com/rehooks/local-storage/b...I put together a tiny react app here: stackblitz.com/edit/react-5ldn5l. Hopefully, this can highlight some of the features that op left out from the original snippet.