Created
February 10, 2019 20:14
-
-
Save dance2die/004c227bd94d574c313cde52868e9cf5 to your computer and use it in GitHub Desktop.
This file contains 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
import React from "react"; | |
export default function useLocalStorage(key: string, initialValue: string = "") { | |
const [item, setValue] = React.useState(() => { | |
const value = localStorage.getItem(key) || initialValue; | |
localStorage.setItem(key, value); | |
return value; | |
}); | |
const setItem = (item: string) => { | |
setValue(item); | |
window.localStorage.setItem(key, item); | |
}; | |
return [item, setItem]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment