Created
June 10, 2020 08:10
-
-
Save hongkheng/f1895822f57958280d8a3535e1e830ba 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 { useState, useEffect } from 'react' | |
export const useLocalStorage = (key, value = undefined) => { | |
const [storeValue, setStoreValue] = useState(() => { | |
// get value if exists in localStorage | |
const item = window.localStorage.getItem(key) | |
return item | |
}) | |
useEffect(() => { | |
window.localStorage.setItem(key, value) | |
}, [key, value]) | |
return { | |
storeValue, | |
setStoreValue | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment