aboutsummaryrefslogtreecommitdiffstats
path: root/src/hooks
diff options
context:
space:
mode:
authorLibravatar Mo Tarbin <mhed.t91@gmail.com>2024-06-30 18:55:39 -0400
committerLibravatar Mo Tarbin <mhed.t91@gmail.com>2024-06-30 18:55:39 -0400
commit2657469964e24ffbeb905024532120395f6e797c (patch)
tree2fe9db8a4ecfa92d854ca94f7586d81163c8bd25 /src/hooks
downloaddonetick-frontend-2657469964e24ffbeb905024532120395f6e797c.tar.gz
donetick-frontend-2657469964e24ffbeb905024532120395f6e797c.tar.bz2
donetick-frontend-2657469964e24ffbeb905024532120395f6e797c.zip
move to Donetick Org, First commit frontend
Diffstat (limited to '')
-rw-r--r--src/hooks/useStickyState.js16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/hooks/useStickyState.js b/src/hooks/useStickyState.js
new file mode 100644
index 0000000..237362a
--- /dev/null
+++ b/src/hooks/useStickyState.js
@@ -0,0 +1,16 @@
+import { useEffect, useState } from 'react'
+
+const useStickyState = (defaultValue, key) => {
+ const [value, setValue] = useState(() => {
+ const stickyValue = window.localStorage.getItem(key)
+ return stickyValue !== null ? JSON.parse(stickyValue) : defaultValue
+ })
+
+ useEffect(() => {
+ window.localStorage.setItem(key, JSON.stringify(value))
+ }, [key, value])
+
+ return [value, setValue]
+}
+
+export default useStickyState