aboutsummaryrefslogtreecommitdiff
path: root/modern/src/common/usePersistedState.js
diff options
context:
space:
mode:
Diffstat (limited to 'modern/src/common/usePersistedState.js')
-rw-r--r--modern/src/common/usePersistedState.js15
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];
+};