Skip to content

Commit 4988f92

Browse files
committed
Add useSessionStorage to main package
1 parent 7f98f53 commit 4988f92

File tree

1 file changed

+66
-4
lines changed

1 file changed

+66
-4
lines changed

index.js

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ function throttle(cb, ms) {
3838
};
3939
}
4040

41+
const dispatchStorageEvent = (key, newValue) => {
42+
window.dispatchEvent(new StorageEvent("storage", { key, newValue }));
43+
};
44+
4145
export function useBattery() {
4246
const [state, setState] = React.useState({
4347
supported: true,
@@ -564,10 +568,6 @@ export function useList(defaultList = []) {
564568
return [list, { set, push, removeAt, insertAt, updateAt, clear }];
565569
}
566570

567-
const dispatchStorageEvent = (key, newValue) => {
568-
window.dispatchEvent(new StorageEvent("storage", { key, newValue }));
569-
};
570-
571571
const setLocalStorageItem = (key, value) => {
572572
const stringifiedValue = JSON.stringify(value);
573573
window.localStorage.setItem(key, stringifiedValue);
@@ -1147,6 +1147,68 @@ export function useScript(src, options = {}) {
11471147
return status;
11481148
}
11491149

1150+
const setSessionStorageItem = (key, value) => {
1151+
const stringifiedValue = JSON.stringify(value);
1152+
window.sessionStorage.setItem(key, stringifiedValue);
1153+
dispatchStorageEvent(key, stringifiedValue);
1154+
};
1155+
1156+
const removeSessionStorageItem = (key) => {
1157+
window.sessionStorage.removeItem(key);
1158+
dispatchStorageEvent(key, null);
1159+
};
1160+
1161+
const getSessionStorageItem = (key) => {
1162+
return window.sessionStorage.getItem(key);
1163+
};
1164+
1165+
const useSessionStorageSubscribe = (callback) => {
1166+
window.addEventListener("storage", callback);
1167+
return () => window.removeEventListener("storage", callback);
1168+
};
1169+
1170+
const getSessionStorageServerSnapshot = () => {
1171+
throw Error("useSessionStorage is a client-only hook");
1172+
};
1173+
1174+
export function useSessionStorage(key, initialValue) {
1175+
const getSnapshot = () => getSessionStorageItem(key);
1176+
1177+
const store = React.useSyncExternalStore(
1178+
useSessionStorageSubscribe,
1179+
getSnapshot,
1180+
getSessionStorageServerSnapshot
1181+
);
1182+
1183+
const setState = React.useCallback(
1184+
(v) => {
1185+
try {
1186+
const nextState = typeof v === "function" ? v(JSON.parse(store)) : v;
1187+
1188+
if (nextState === undefined || nextState === null) {
1189+
removeSessionStorageItem(key);
1190+
} else {
1191+
setSessionStorageItem(key, nextState);
1192+
}
1193+
} catch (e) {
1194+
console.warn(e);
1195+
}
1196+
},
1197+
[key, store]
1198+
);
1199+
1200+
React.useEffect(() => {
1201+
if (
1202+
getSessionStorageItem(key) === null &&
1203+
typeof initialValue !== "undefined"
1204+
) {
1205+
setSessionStorageItem(key, initialValue);
1206+
}
1207+
}, [key, initialValue]);
1208+
1209+
return [store ? JSON.parse(store) : initialValue, setState];
1210+
}
1211+
11501212
export function useSet(values) {
11511213
const setRef = React.useRef(new Set(values));
11521214
const [, reRender] = React.useReducer((x) => x + 1, 0);

0 commit comments

Comments
 (0)