@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.
Files changed (2) hide show
  1. package/dist/hooks/index.js +11 -13
  2. package/package.json +1 -1
@@ -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
- let newValue;
159
- if (typeof valueOrFn === 'function') {
160
- newValue = valueOrFn(storedValue);
161
- }
162
- else {
163
- newValue = valueOrFn;
164
- }
165
- sessionStorage.setItem(key, JSON.stringify(newValue));
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
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sohanemon/utils",
3
- "version": "5.1.3",
3
+ "version": "5.1.4",
4
4
  "author": "Sohan Emon <sohanemon@outlook.com>",
5
5
  "description": "",
6
6
  "type": "module",