@sohanemon/utils 4.1.8 → 4.1.9
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 +11 -0
- package/dist/hooks/index.js +27 -0
- package/package.json +1 -1
package/dist/hooks/index.d.ts
CHANGED
|
@@ -47,6 +47,17 @@ export declare function useTimeout(callback: () => void, delay?: number | null):
|
|
|
47
47
|
* @param options - Options for the event listener.
|
|
48
48
|
*/
|
|
49
49
|
export declare function useWindowEvent<K extends keyof WindowEventMap>(type: K, listener: (this: Window, ev: WindowEventMap[K]) => void, options?: boolean | AddEventListenerOptions): void;
|
|
50
|
+
/**
|
|
51
|
+
* Tuple type for session storage value and updater.
|
|
52
|
+
*/
|
|
53
|
+
type SessionStorageValue<T> = [T, React.Dispatch<React.SetStateAction<T>>];
|
|
54
|
+
/**
|
|
55
|
+
* Hook to persist state in session storage.
|
|
56
|
+
* @param key - The key for session storage.
|
|
57
|
+
* @param defaultValue - The default value if no value is found in session storage.
|
|
58
|
+
* @returns A tuple of the stored value and an updater function.
|
|
59
|
+
*/
|
|
60
|
+
export declare const useSessionStorage: <T extends Record<string, any>>(key: string, defaultValue: T) => SessionStorageValue<T>;
|
|
50
61
|
/**
|
|
51
62
|
* Tuple type for local storage value and updater.
|
|
52
63
|
*/
|
package/dist/hooks/index.js
CHANGED
|
@@ -140,6 +140,33 @@ export function useWindowEvent(type, listener, options) {
|
|
|
140
140
|
return () => window.removeEventListener(type, listener, options);
|
|
141
141
|
}, [type, listener, options]);
|
|
142
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* Hook to persist state in session storage.
|
|
145
|
+
* @param key - The key for session storage.
|
|
146
|
+
* @param defaultValue - The default value if no value is found in session storage.
|
|
147
|
+
* @returns A tuple of the stored value and an updater function.
|
|
148
|
+
*/
|
|
149
|
+
export const useSessionStorage = (key, defaultValue) => {
|
|
150
|
+
const [storedValue, setStoredValue] = React.useState(defaultValue);
|
|
151
|
+
React.useEffect(() => {
|
|
152
|
+
const value = sessionStorage.getItem(key);
|
|
153
|
+
if (value) {
|
|
154
|
+
setStoredValue(JSON.parse(value));
|
|
155
|
+
}
|
|
156
|
+
}, [key]);
|
|
157
|
+
const updateStoredValue = (valueOrFn) => {
|
|
158
|
+
let newValue;
|
|
159
|
+
if (typeof valueOrFn === 'function') {
|
|
160
|
+
newValue = valueOrFn(storedValue);
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
newValue = valueOrFn;
|
|
164
|
+
}
|
|
165
|
+
sessionStorage.setItem(key, JSON.stringify(newValue));
|
|
166
|
+
setStoredValue(newValue);
|
|
167
|
+
};
|
|
168
|
+
return [storedValue, updateStoredValue];
|
|
169
|
+
};
|
|
143
170
|
/**
|
|
144
171
|
* Hook to persist state in local storage.
|
|
145
172
|
* @param key - The key for local storage.
|