@sohanemon/utils 4.0.3 → 4.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hooks/index.d.ts +4 -1
- package/dist/hooks/index.js +25 -0
- package/package.json +1 -1
package/dist/hooks/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EffectCallback } from 'react';
|
|
1
|
+
import { Dispatch, EffectCallback, SetStateAction } from 'react';
|
|
2
2
|
export declare const useClickOutside: (callback?: () => void) => any;
|
|
3
3
|
export declare function useMediaQuery(tailwindBreakpoint: 'sm' | 'md' | 'lg' | 'xl' | '2xl' | `(${string})`): any;
|
|
4
4
|
export declare function useEffectOnce(effect: EffectCallback): void;
|
|
@@ -7,3 +7,6 @@ export declare function useDebounce<T>(state: T, delay?: number): T;
|
|
|
7
7
|
export declare const useIsomorphicEffect: any;
|
|
8
8
|
export declare function useTimeout(callback: () => void, delay?: number | null): void;
|
|
9
9
|
export declare function useWindowEvent<K extends string = keyof WindowEventMap>(type: K, listener: K extends keyof WindowEventMap ? (this: Window, ev: WindowEventMap[K]) => void : (this: Window, ev: CustomEvent) => void, options?: boolean | AddEventListenerOptions): void;
|
|
10
|
+
type LocalStorageValue<T> = [T, Dispatch<SetStateAction<T>>];
|
|
11
|
+
export declare const useLocalStorage: <T extends Record<string, any>>(key: string, defaultValue: T) => LocalStorageValue<T>;
|
|
12
|
+
export {};
|
package/dist/hooks/index.js
CHANGED
|
@@ -101,3 +101,28 @@ export function useWindowEvent(type, listener, options) {
|
|
|
101
101
|
return () => window.removeEventListener(type, listener, options);
|
|
102
102
|
}, [type, listener]);
|
|
103
103
|
}
|
|
104
|
+
// Custom hook for using local storage with a specified key and default value
|
|
105
|
+
export const useLocalStorage = (key, defaultValue) => {
|
|
106
|
+
const [storedValue, setStoredValue] = useState(defaultValue);
|
|
107
|
+
// Use effect to retrieve the stored value from local storage on component mount
|
|
108
|
+
useEffect(() => {
|
|
109
|
+
const value = localStorage.getItem(key);
|
|
110
|
+
if (value) {
|
|
111
|
+
setStoredValue(JSON.parse(value));
|
|
112
|
+
}
|
|
113
|
+
}, [key]);
|
|
114
|
+
// Function to update the stored value in local storage and state
|
|
115
|
+
const updateStoredValue = (valueOrFn) => {
|
|
116
|
+
let newValue;
|
|
117
|
+
if (typeof valueOrFn === 'function') {
|
|
118
|
+
const updateFunction = valueOrFn;
|
|
119
|
+
newValue = updateFunction(storedValue);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
newValue = valueOrFn;
|
|
123
|
+
}
|
|
124
|
+
localStorage.setItem(key, JSON.stringify(newValue));
|
|
125
|
+
setStoredValue(newValue);
|
|
126
|
+
};
|
|
127
|
+
return [storedValue, updateStoredValue];
|
|
128
|
+
};
|