diff --git a/power-pay-frontend/src/hooks/useStorage.ts b/power-pay-frontend/src/hooks/useStorage.ts index 3f39c21e..98a02dd2 100644 --- a/power-pay-frontend/src/hooks/useStorage.ts +++ b/power-pay-frontend/src/hooks/useStorage.ts @@ -1,34 +1,25 @@ //useState and useEffect hooks from React -import { useEffect, useState } from 'react'; +import { useState } from 'react'; //UseStorageProps: An interface defining the props for the useStorage hook interface UseStorageProps { - key: string; - initialValue?: T | undefined ; + initialValue?: T ; } //useStorage: A generic function that accepts a UseStorageProps object. export function useStorage(props: UseStorageProps): [T, React.Dispatch> , () => void] { - const { key, initialValue } = props; + const { initialValue } = props; //storedValue: A state variable that stores the value retrieved from the localStorage using the provided key - const [storedValue, setStoredValue] = useState(() => { - const item = localStorage.getItem(key); - return item ? JSON.parse(item) : initialValue - }); - - //runs when the storedValue changes. It updates the localStorage - useEffect(() => { - localStorage.setItem(key, JSON.stringify(storedValue)); - }, [key, storedValue]); + const [storedValue, setStoredValue] = useState(initialValue!); +; //adding a newfunction removeValue to remove the stored value from the localstorage and set it to its initial state const removeValue = () => { - localStorage.removeItem(key); setStoredValue(initialValue!); };