mobx-react-use-autorun 3.0.0 → 3.0.1

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.
@@ -2,15 +2,16 @@ import { extendObservable, remove, isObservable } from 'mobx';
2
2
  import { useLocalObservable } from 'mobx-react-lite';
3
3
  export function useMobxState(state, props) {
4
4
  var _a, _b;
5
- var mobxState = useLocalObservable(function () { return state; });
6
- for (var key in props) {
7
- if (isObservable(props[key])) {
8
- mobxState[key] = props[key];
5
+ var mobxState = useLocalObservable(typeof state === "function" ? state : function () { return state; });
6
+ var propsResult = typeof props === "function" ? props() : props;
7
+ for (var key in propsResult) {
8
+ if (isObservable(propsResult[key])) {
9
+ mobxState[key] = propsResult[key];
9
10
  }
10
11
  else {
11
- if (props[key] !== mobxState[key]) {
12
+ if (propsResult[key] !== mobxState[key]) {
12
13
  remove(mobxState, key);
13
- extendObservable(mobxState, (_a = {}, _a[key] = props[key], _a), (_b = {}, _b[key] = false, _b));
14
+ extendObservable(mobxState, (_a = {}, _a[key] = propsResult[key], _a), (_b = {}, _b[key] = false, _b));
14
15
  }
15
16
  }
16
17
  }
@@ -1 +1 @@
1
- {"version":3,"file":"useMobxState.js","sourceRoot":"","sources":["../../lib/lib/useMobxState.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAErD,MAAM,UAAU,YAAY,CAAyD,KAAQ,EAAE,KAAS;;IAEpG,IAAM,SAAS,GAAG,kBAAkB,CAAC,cAAM,OAAA,KAAK,EAAL,CAAK,CAAC,CAAC;IAElD,KAAK,IAAM,GAAG,IAAI,KAAK,EAAE;QACrB,IAAI,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE;YAC1B,SAAS,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,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G,KAAK,MAAG,CAAA;aAC7E;SACJ;KACJ;IAED,OAAO,SAAgB,CAAC;AAC5B,CAAC"}
package/lib/index.d.ts CHANGED
@@ -1,2 +1,6 @@
1
- declare const useMobxState: (fn: () => any) => void;
1
+ declare function useMobxState<T extends Record<any, any>, P extends Record<any, any>>(state: T, props?: P): T & P;
2
+ declare function useMobxState<T extends Record<any, any>, P extends Record<any, any>>(statefn: () => T, propsfn: () => P): T & P;
3
+ declare function useMobxState<T extends Record<any, any>, P extends Record<any, any>>(statefn: () => T, props?: P): T & P;
4
+ declare function useMobxState<T extends Record<any, any>, P extends Record<any, any>>(state: T, propsfn: () => P): T & P;
5
+
2
6
  export { useMobxState };
@@ -3,18 +3,20 @@ import { useLocalObservable } from 'mobx-react-lite';
3
3
 
4
4
  export function useMobxState<T extends Record<any, any>, P extends Record<any, any>>(state: T, props?: P): T & P {
5
5
 
6
- const mobxState = useLocalObservable(() => state);
6
+ const mobxState = useLocalObservable(typeof state === "function" ? (state as any) : () => state);
7
7
 
8
- for (const key in props) {
9
- if (isObservable(props[key])) {
10
- mobxState[key] = props[key];
8
+ const propsResult = typeof props === "function" ? props() : props;
9
+
10
+ for (const key in propsResult) {
11
+ if (isObservable(propsResult[key])) {
12
+ mobxState[key] = propsResult[key];
11
13
  } else {
12
- if (props[key] !== mobxState[key]) {
14
+ if (propsResult[key] !== mobxState[key]) {
13
15
  remove(mobxState, key);
14
- extendObservable(mobxState, { [key]: props[key] }, { [key]: false })
16
+ extendObservable(mobxState, { [key]: propsResult[key] }, { [key]: false })
15
17
  }
16
18
  }
17
19
  }
18
20
 
19
- return mobxState;
21
+ return mobxState as any;
20
22
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobx-react-use-autorun",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
4
4
  "description": "React Hook for mobx",
5
5
  "scripts": {
6
6
  "build": "npm install -g typescript@4.6.3 @types/node@17.0.23 ts-node@10.7.0 cross-env@7.0.3 && cross-env TS_NODE_SKIP_PROJECT=true ts-node bin/mobx_react_build.ts",