Skip to content

Commit

Permalink
fix: updated removeValue
Browse files Browse the repository at this point in the history
  • Loading branch information
AssahBismarkabah committed Mar 12, 2024
1 parent 7974f91 commit 48933b5
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions power-pay-frontend/src/hooks/useStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { useEffect, useState } from 'react';
//UseStorageProps<T>: An interface defining the props for the useStorage hook
interface UseStorageProps<T> {
key: string;
initialValue?: T;
initialValue?: T | undefined ;
}

//useStorage<T = string>: A generic function that accepts a UseStorageProps<T> object.

export function useStorage<T = string>(props: UseStorageProps<T>): [T, React.Dispatch<React.SetStateAction<T>>] {
export function useStorage<T = string>(props: UseStorageProps<T>): [T, React.Dispatch<React.SetStateAction<T>> , () => void] {

const { key, initialValue } = props;

Expand All @@ -25,5 +25,12 @@ export function useStorage<T = string>(props: UseStorageProps<T>): [T, React.Dis
localStorage.setItem(key, JSON.stringify(storedValue));
}, [key, storedValue]);

return [storedValue, setStoredValue];
//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!);
};

return [storedValue, setStoredValue , removeValue];
}

0 comments on commit 48933b5

Please sign in to comment.