I was manually updating localStorage anytime state was updated


I was manually updating localStorage anytime state was updated. Then I realized: That's the perfect job for useEffect.

Here, I’m persisting shopping cart data to localStorage via a single line of code in useEffect. Now the concern is handled in one spot. 👍

#reactjs

Oh, and here's an elegant custom hook that does this behind the scenes for you.

https://usehooks.com/useLocalStorage/

View original on X