@bouko/react 3.1.5 → 3.1.6
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.d.ts +8 -0
- package/dist/hooks/index.js +12 -0
- package/package.json +1 -1
package/dist/hooks/index.d.ts
CHANGED
|
@@ -23,3 +23,11 @@ export declare function useValue<T>({ fetcher, updater }: Props<T>): {
|
|
|
23
23
|
value: T | null;
|
|
24
24
|
update: (x?: T) => Promise<void>;
|
|
25
25
|
};
|
|
26
|
+
type ConfigProps<T> = {
|
|
27
|
+
fetcher: () => Promise<Record<string, T> | null>;
|
|
28
|
+
updater: (x: string, value: T) => Promise<T>;
|
|
29
|
+
};
|
|
30
|
+
export declare function useConfig<T>({ fetcher, updater }: ConfigProps<T>): {
|
|
31
|
+
config: Record<string, T>;
|
|
32
|
+
update: (id: string, x: T) => Promise<void>;
|
|
33
|
+
};
|
package/dist/hooks/index.js
CHANGED
|
@@ -42,3 +42,15 @@ export function useValue({ fetcher, updater }) {
|
|
|
42
42
|
};
|
|
43
43
|
return { value, update };
|
|
44
44
|
}
|
|
45
|
+
export function useConfig({ fetcher, updater }) {
|
|
46
|
+
const [config, setConfig] = useState({});
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
fetcher()
|
|
49
|
+
.then(x => setConfig(x || {}));
|
|
50
|
+
}, []);
|
|
51
|
+
const update = async (id, x) => {
|
|
52
|
+
const res = await updater(id, x);
|
|
53
|
+
setConfig(prev => ({ ...prev, [id]: res }));
|
|
54
|
+
};
|
|
55
|
+
return { config, update };
|
|
56
|
+
}
|