@@ -639,16 +639,22 @@ export function useLocalStorage(key, initialValue) {
639
639
[ key , store ]
640
640
) ;
641
641
642
+ const initialValueRef = React . useRef ( initialValue ) ;
643
+
642
644
React . useEffect ( ( ) => {
643
645
if (
644
646
getLocalStorageItem ( key ) === null &&
645
- typeof initialValue !== "undefined"
647
+ typeof initialValueRef . current !== "undefined"
646
648
) {
647
- setLocalStorageItem ( key , initialValue ) ;
649
+ setLocalStorageItem ( key , initialValueRef . current ) ;
648
650
}
649
- } , [ key , initialValue ] ) ;
651
+ } , [ key ] ) ;
652
+
653
+ const result = React . useMemo ( ( ) => {
654
+ return store ? JSON . parse ( store ) : initialValueRef . current ;
655
+ } , [ store ] ) ;
650
656
651
- return [ store ? JSON . parse ( store ) : initialValue , setState ] ;
657
+ return [ result , setState ] ;
652
658
}
653
659
654
660
export function useLockBodyScroll ( ) {
@@ -1200,16 +1206,22 @@ export function useSessionStorage(key, initialValue) {
1200
1206
[ key , store ]
1201
1207
) ;
1202
1208
1209
+ const initialValueRef = React . useRef ( initialValue ) ;
1210
+
1203
1211
React . useEffect ( ( ) => {
1204
1212
if (
1205
1213
getSessionStorageItem ( key ) === null &&
1206
- typeof initialValue !== "undefined"
1214
+ typeof initialValueRef . current !== "undefined"
1207
1215
) {
1208
- setSessionStorageItem ( key , initialValue ) ;
1216
+ setSessionStorageItem ( key , initialValueRef . current ) ;
1209
1217
}
1210
- } , [ key , initialValue ] ) ;
1218
+ } , [ key ] ) ;
1219
+
1220
+ const result = React . useMemo ( ( ) => {
1221
+ return store ? JSON . parse ( store ) : initialValueRef . current ;
1222
+ } , [ store ] ) ;
1211
1223
1212
- return [ store ? JSON . parse ( store ) : initialValue , setState ] ;
1224
+ return [ result , setState ] ;
1213
1225
}
1214
1226
1215
1227
export function useSet ( values ) {
0 commit comments