@@ -81,7 +81,21 @@ export const useStorage = <Value>(
81
81
const options = ( typeof params === 'object' ? params : undefined ) as UseStorageOptions < Value > ;
82
82
const initialValue = ( options ? options ?. initialValue : params ) as UseStorageInitialValue < Value > ;
83
83
84
- const storage = options ?. storage ?? window . localStorage ;
84
+ const set = ( value : Value ) => {
85
+ if ( value === null ) return removeStorageItem ( storage , key ) ;
86
+ setStorageItem ( storage , key , serializer ( value ) ) ;
87
+ } ;
88
+ const remove = ( ) => removeStorageItem ( storage , key ) ;
89
+
90
+
91
+ if ( ! isClient )
92
+ return {
93
+ value : initialValue instanceof Function ? initialValue ( ) : initialValue ,
94
+ set,
95
+ remove
96
+ } ;
97
+
98
+ const storage = options ?. storage ?? window ?. localStorage ;
85
99
const serializer = ( value : Value ) => {
86
100
if ( options ?. serializer ) return options . serializer ( value ) ;
87
101
return JSON . stringify ( value ) ;
@@ -104,11 +118,6 @@ export const useStorage = <Value>(
104
118
const getSnapshot = ( ) => getStorageItem ( storage , key ) ;
105
119
const store = useSyncExternalStore ( storageSubscribe , getSnapshot , getServerSnapshot ) ;
106
120
107
- const set = ( value : Value ) => {
108
- if ( value === null ) return removeStorageItem ( storage , key ) ;
109
- setStorageItem ( storage , key , serializer ( value ) ) ;
110
- } ;
111
-
112
121
useIsomorphicLayoutEffect ( ( ) => {
113
122
const value = getStorageItem ( storage , key ) ;
114
123
if ( value === undefined && initialValue !== undefined ) {
@@ -120,14 +129,7 @@ export const useStorage = <Value>(
120
129
}
121
130
} , [ key ] ) ;
122
131
123
- const remove = ( ) => removeStorageItem ( storage , key ) ;
124
132
125
- if ( ! isClient )
126
- return {
127
- value : initialValue instanceof Function ? initialValue ( ) : initialValue ,
128
- set,
129
- remove
130
- } ;
131
133
return {
132
134
value : store ? deserializer ( store ) : undefined ,
133
135
set,
0 commit comments