@@ -38,6 +38,10 @@ function throttle(cb, ms) {
38
38
} ;
39
39
}
40
40
41
+ const dispatchStorageEvent = ( key , newValue ) => {
42
+ window . dispatchEvent ( new StorageEvent ( "storage" , { key, newValue } ) ) ;
43
+ } ;
44
+
41
45
export function useBattery ( ) {
42
46
const [ state , setState ] = React . useState ( {
43
47
supported : true ,
@@ -564,10 +568,6 @@ export function useList(defaultList = []) {
564
568
return [ list , { set, push, removeAt, insertAt, updateAt, clear } ] ;
565
569
}
566
570
567
- const dispatchStorageEvent = ( key , newValue ) => {
568
- window . dispatchEvent ( new StorageEvent ( "storage" , { key, newValue } ) ) ;
569
- } ;
570
-
571
571
const setLocalStorageItem = ( key , value ) => {
572
572
const stringifiedValue = JSON . stringify ( value ) ;
573
573
window . localStorage . setItem ( key , stringifiedValue ) ;
@@ -1147,6 +1147,68 @@ export function useScript(src, options = {}) {
1147
1147
return status ;
1148
1148
}
1149
1149
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
+
1150
1212
export function useSet ( values ) {
1151
1213
const setRef = React . useRef ( new Set ( values ) ) ;
1152
1214
const [ , reRender ] = React . useReducer ( ( x ) => x + 1 , 0 ) ;
0 commit comments