Skip to content

Commit

Permalink
#: Upgrade version to 4.0.34-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
zdu-strong committed Jun 22, 2024
1 parent abc2b7f commit 70ee837
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
25 changes: 13 additions & 12 deletions lib/js/useMobxState.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { remove, isObservable, runInAction } from 'mobx';
import { useLocalObservable } from 'mobx-react-lite';
import { useRef } from 'react';
import { remove, runInAction, observable } from 'mobx';
import { useRef, useState } from 'react';

export function useMobxState<T extends Record<any, any>>(state: T | (() => T)): T;
export function useMobxState<T extends Record<any, any>, P extends Record<any, any>>(state: T | (() => T), props: P): T & P;

export function useMobxState<T extends Record<any, any>, P extends Record<any, any>>(state: T | (() => T), props?: P): T & P {
const mobxState = useLocalObservable(typeof state === "function" ? (state as any) : () => state);
const initStateFunction = typeof state === "function" ? (state as any) : () => state;
const initState = useState(initStateFunction)[0];
const initAnnotations = {} as any;
for (const initStateKey of Object.keys(initState)) {
initAnnotations[initStateKey] = observable.deep;
}

const mobxState = useState(() => observable(initState, initAnnotations, { autoBind: true, proxy: false }))[0] as Record<string, any>;

const keyListOfState = useRef<string[]>([]);

Expand All @@ -21,17 +27,12 @@ export function useMobxState<T extends Record<any, any>, P extends Record<any, a
}

for (const key of Object.keys(props)) {
if (isObservable(props[key]) || Object.getOwnPropertyDescriptor(props, key)?.get) {
Object.defineProperty(mobxState, key, Object.getOwnPropertyDescriptor(props, key) as any)
} else {
if (props[key] !== mobxState[key]) {
Object.defineProperty(mobxState, key, Object.getOwnPropertyDescriptor(props, key) as any)
}
}
remove(mobxState, key);
Object.assign(mobxState, { [key]: props[key] });
}

keyListOfState.current.splice(0, keyListOfState.current.length);
for (const key in props) {
for (const key of Object.keys(props)) {
keyListOfState.current.push(key);
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mobx-react-use-autorun",
"version": "4.0.33-beta",
"version": "4.0.34-beta",
"description": "Provide concise usage for mobx in react",
"scripts": {
"test": "npx -y -p typescript -p ts-node ts-node --skipProject bin/mobx_react_test.ts",
Expand Down

0 comments on commit 70ee837

Please sign in to comment.