Categories
React Answers

How to listen to local storage value changes in React?

Spread the love

To listen to local storage value changes in React, we listen to the storage event.

For instance, we write

window.addEventListener("storage", (e) => {
  this.setState({ auth: true });
});

to listen to the storage event by calling window.addEventListener with 'storage'.

And then we do whatever we want when it changes by calling it with a callback that runs when the event is triggered.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.