Created
January 29, 2022 15:54
-
-
Save mcnaveen/52b130146b5be057151d9ff95c138d59 to your computer and use it in GitHub Desktop.
Persist state after a page refresh in React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const storedValueAsNumber = Number(localStorage.getItem("limit")); | |
const [userLimit, setUserLimit] = useState( | |
Number.isInteger(storedValueAsNumber) ? storedValueAsNumber : 0 | |
); | |
useEffect(() => { | |
localStorage.setItem("limit", String(userLimit)); | |
}, [userLimit]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment