DEV Community

Discussion on: Top 10 React Hook libraries

 
steventhan profile image
Steven Than

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.