~repos /atoms-state

#js#react#flux

git clone https://pyrossh.dev/repos/atoms-state.git

Simple State management for react


c8aa1af1 Peter John

tag: v0.7.2

v0.7.2

3 years ago
fix useAsyncStorage
Files changed (3) hide show
  1. index.d.ts +1 -1
  2. index.js +6 -5
  3. package.json +1 -1
index.d.ts CHANGED
@@ -52,4 +52,4 @@ declare namespace Storage {
52
52
  export function unsubscribe(key: string, cb: Function): void;
53
53
  }
54
54
 
55
- export function useAsyncStorage<T>(key: string, initial: T): [T, (v: T) => void, boolean];
55
+ export function useAsyncStorage<T>(key: string, initial: T & { loading?: boolean }): [T, (v: T) => void];
index.js CHANGED
@@ -172,8 +172,10 @@ export const Storage = {
172
172
  };
173
173
 
174
174
  export const useAsyncStorage = (key, initialValue) => {
175
- const [loading, setLoading] = useState(true);
176
- const [storedValue, setStoredValue] = useState(initialValue);
175
+ const [storedValue, setStoredValue] = useState({
176
+ ...initialValue,
177
+ loading: true,
178
+ });
177
179
  const setData = async (v) => {
178
180
  await Storage.setItem(key, v);
179
181
  };
@@ -183,14 +185,13 @@ export const useAsyncStorage = (key, initialValue) => {
183
185
  if (v) {
184
186
  setData(v);
185
187
  }
186
- setLoading(false);
187
188
  })
188
- .catch(() => setLoading(false));
189
+ .catch(() => setStoredValue({ ...storedValue, loading: false }));
189
190
  return Storage.subscribe(key, (v) => {
190
191
  setStoredValue(v);
191
192
  });
192
193
  }, [key, JSON.stringify(initialValue)]);
193
- return [storedValue || initialValue, setData, loading];
194
+ return [storedValue, setData];
194
195
  };
195
196
 
196
197
  export const fields = {};
package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "atoms-state",
3
3
  "author": "pyros.sh",
4
4
  "description": "State management and common hooks",
5
- "version": "0.7.1",
5
+ "version": "0.7.2",
6
6
  "license": "MIT",
7
7
  "type": "module",
8
8
  "main": "index.js",