Created
January 4, 2020 08:39
-
-
Save arekko/7d080d41527b832c9939dd8529194075 to your computer and use it in GitHub Desktop.
useLocalStorage hook implementation
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
export (key, initialValue) => { | |
const [value, setValue] = useState(() => { | |
return localStorage.getItem(key) || initialValue | |
}) | |
useEffect(() => { | |
localStorage.setItem(key, value) | |
}, [value]) | |
return [value, setValue] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment