@sohanemon/utils 5.1.3 → 5.1.4
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.js +11 -13
- package/package.json +1 -1
package/dist/hooks/index.js
CHANGED
|
@@ -154,17 +154,15 @@ export const useSessionStorage = (key, defaultValue) => {
|
|
|
154
154
|
setStoredValue(JSON.parse(value));
|
|
155
155
|
}
|
|
156
156
|
}, [key]);
|
|
157
|
-
const updateStoredValue = (valueOrFn) => {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
newValue
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
setStoredValue(newValue);
|
|
167
|
-
};
|
|
157
|
+
const updateStoredValue = React.useCallback((valueOrFn) => {
|
|
158
|
+
setStoredValue((prev) => {
|
|
159
|
+
const newValue = typeof valueOrFn === 'function'
|
|
160
|
+
? valueOrFn(prev)
|
|
161
|
+
: valueOrFn;
|
|
162
|
+
sessionStorage.setItem(key, JSON.stringify(newValue));
|
|
163
|
+
return newValue;
|
|
164
|
+
});
|
|
165
|
+
}, [key]);
|
|
168
166
|
return [storedValue, updateStoredValue];
|
|
169
167
|
};
|
|
170
168
|
/**
|
|
@@ -181,7 +179,7 @@ export const useLocalStorage = (key, defaultValue) => {
|
|
|
181
179
|
setStoredValue(JSON.parse(value));
|
|
182
180
|
}
|
|
183
181
|
}, [key]);
|
|
184
|
-
const updateStoredValue = (valueOrFn) => {
|
|
182
|
+
const updateStoredValue = React.useCallback((valueOrFn) => {
|
|
185
183
|
let newValue;
|
|
186
184
|
if (typeof valueOrFn === 'function') {
|
|
187
185
|
newValue = valueOrFn(storedValue);
|
|
@@ -191,7 +189,7 @@ export const useLocalStorage = (key, defaultValue) => {
|
|
|
191
189
|
}
|
|
192
190
|
localStorage.setItem(key, JSON.stringify(newValue));
|
|
193
191
|
setStoredValue(newValue);
|
|
194
|
-
};
|
|
192
|
+
}, [key]);
|
|
195
193
|
return [storedValue, updateStoredValue];
|
|
196
194
|
};
|
|
197
195
|
/**
|