From c50d41c53ddb48e613e8c653ddab1892f7b6331c Mon Sep 17 00:00:00 2001 From: Hasibur Rahman Date: Sun, 24 Nov 2024 17:34:07 +0600 Subject: [PATCH] refactor: simplify useLocalStorage hook and improve error handling --- src/hooks/index.tsx | 60 +++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/src/hooks/index.tsx b/src/hooks/index.tsx index 42e119f..aa571cf 100644 --- a/src/hooks/index.tsx +++ b/src/hooks/index.tsx @@ -1,11 +1,4 @@ -import { - Dispatch, - SetStateAction, - useContext, - useEffect, - useRef, - useState, -} from "react"; +import { useContext, useEffect, useState } from "react"; import { TaskContext } from "../contexts/task-context"; export function useTaskContext() { @@ -16,42 +9,45 @@ export function useTaskContext() { return context; } -type InitialValueType = T | (() => T); - -export function useLocalStorage( - key: string, - initialValue: InitialValueType, - { serialize = JSON.stringify, deserialize = JSON.parse } = {} -): [T, Dispatch>] { +export function useLocalStorage(key: string, initialValue: T) { + // State to store the value const [storedValue, setStoredValue] = useState(() => { try { - const item = window.localStorage.getItem(key); - if (item) { - return deserialize(item); - } - - return initialValue instanceof Function ? initialValue() : initialValue; + // Check if the key exists in localStorage + const item = localStorage.getItem(key); + return item ? JSON.parse(item) : initialValue; } catch (error) { - console.error(error); + console.error("Error reading from localStorage:", error); return initialValue; } }); - const prevKeyRef = useRef(key); + // Function to update the value in state and localStorage + const setValue = (value: T | ((val: T) => T)) => { + try { + // Allow value to be a function for functional updates + const valueToStore = + value instanceof Function ? value(storedValue) : value; + // Save state + setStoredValue(valueToStore); + // Save to localStorage + localStorage.setItem(key, JSON.stringify(valueToStore)); + } catch (error) { + console.error("Error writing to localStorage:", error); + } + }; - // Use useEffect to update localstorage when value changes + // Sync state with localStorage if `key` changes useEffect(() => { try { - if (prevKeyRef.current !== key) { - window.localStorage.removeItem(prevKeyRef.current); + const item = localStorage.getItem(key); + if (item) { + setStoredValue(JSON.parse(item)); } - - prevKeyRef.current = key; - window.localStorage.setItem(key, serialize(storedValue)); } catch (error) { - console.error(error); + console.error("Error reading from localStorage on key change:", error); } - }, [storedValue, serialize, key]); + }, [key]); - return [storedValue, setStoredValue]; + return [storedValue, setValue] as const; }