diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2021-09-02 23:00:37 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2021-09-02 23:00:37 -0700 |
commit | 26f4e5835d5fb2f0b5035837e2e90ef012b242f0 (patch) | |
tree | d923e21567684dea67c7c89557fefcf684b292d8 /modern/src/common/usePersistedState.js | |
parent | 10cc10e0e88569cc48d86e9ab4d90538683bfaf6 (diff) | |
download | trackermap-web-26f4e5835d5fb2f0b5035837e2e90ef012b242f0.tar.gz trackermap-web-26f4e5835d5fb2f0b5035837e2e90ef012b242f0.tar.bz2 trackermap-web-26f4e5835d5fb2f0b5035837e2e90ef012b242f0.zip |
Reimplement localization provider
Diffstat (limited to 'modern/src/common/usePersistedState.js')
-rw-r--r-- | modern/src/common/usePersistedState.js | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/modern/src/common/usePersistedState.js b/modern/src/common/usePersistedState.js new file mode 100644 index 00000000..0a6627c6 --- /dev/null +++ b/modern/src/common/usePersistedState.js @@ -0,0 +1,15 @@ +import { useState } from 'react'; + +export default usePersistedState = (key, defaultValue) => { + + const [value, setValue] = useState(() => { + const stickyValue = window.localStorage.getItem(key); + return stickyValue ? JSON.parse(stickyValue) : defaultValue; + }); + + React.useEffect(() => { + window.localStorage.setItem(key, JSON.stringify(value)); + }, [key, value]); + + return [value, setValue]; +}; |