Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

add hooks useLocalstorage

Maksim Mrug6e397f5b4ad7
diff --git a/app/hooks/useLocalStorage.ts b/app/hooks/useLocalStorage.ts
new file mode 100644
index 000000000..505e39240
--- /dev/null
+++ b/app/hooks/useLocalStorage.ts
@@ -0,0 +1,21 @@
+import { useEffect, useState } from 'react'
+
+const useLocalStorage = (key: string) => {
+ const [state, setState] = useState(null)
+
+ useEffect(() => {
+ const localState = localStorage.getItem(key)
+
+ if (localState) {
+ setState(JSON.parse(localState))
+ }
+ }, [key])
+
+ const setWithLocalStorage = (nextState: any) => {
+ setState(nextState)
+ }
+
+ return [state, setWithLocalStorage]
+}
+
+export default useLocalStorage