Preserve State in localStorage
Loading "Preserve State in Localstorage"
Preserve State in Localstorage
Run locally for transcripts
π¨βπΌ Our customers want to be able to close the tab in the middle of a game and
then resume the game later. Can you store the game's state in
localStorage?I think you can! You're going to need to use
useEffect to coordinate things
with localStorage. Luckily for us, localStorage is synchronous, so
initializing our state from localStorage is straightforward (you should
definitely use the callback form of useState for initialization though!).For keeping the squares up-to-date in
localStorage, you'll want to use
useEffect with a dependency array that includes the squares.π If you need to learn a bit about the
localStorage API, you can check out the
MDN documentation.π¨ Note this exercise depends on
localStorage and so the tests could
interfer with your work by changing the localStorage you're working with.