mobx-react-use-autorun 3.1.3 → 3.1.9
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/README.md +34 -34
- package/dist/js/mobx_config.js.map +1 -1
- package/dist/js/useMobxEffect.js.map +1 -1
- package/dist/js/useMobxState.js +12 -0
- package/dist/js/useMobxState.js.map +1 -1
- package/lib/js/mobx_config.tsx +1 -1
- package/lib/js/useMobxEffect.tsx +20 -20
- package/lib/js/useMobxState.tsx +32 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ Provide concise usage for mobx in react<br/>
|
|
|
6
6
|
|
|
7
7
|
# `Usage`
|
|
8
8
|
|
|
9
|
-
###
|
|
9
|
+
### Define state and props with useMobxState
|
|
10
10
|
|
|
11
11
|
import { useMobxState, observer } from 'mobx-react-use-autorun';
|
|
12
12
|
import { useRef } from 'react';
|
|
@@ -110,7 +110,7 @@ The second:
|
|
|
110
110
|
|
|
111
111
|
Provide a method to generate state, the state is executed only once, and the performance is better.<br/>
|
|
112
112
|
|
|
113
|
-
###
|
|
113
|
+
### Subscription property changes with useMobxEffect
|
|
114
114
|
|
|
115
115
|
import { useMobxState, observer } from 'mobx-react-use-autorun';
|
|
116
116
|
import { useMobxEffect, toJS } from 'mobx-react-use-autorun'
|
|
@@ -128,7 +128,37 @@ Provide a method to generate state, the state is executed only once, and the per
|
|
|
128
128
|
</div>
|
|
129
129
|
})
|
|
130
130
|
|
|
131
|
-
###
|
|
131
|
+
### Lifecycle hooks
|
|
132
|
+
|
|
133
|
+
useMount is executed when the component loaded.<br/>
|
|
134
|
+
useUnmount is executed when the component is unmount.<br/>
|
|
135
|
+
|
|
136
|
+
import { Subscription, of, tap } from 'rxjs'
|
|
137
|
+
import { useMobxState, observer } from 'mobx-react-use-autorun'
|
|
138
|
+
import { useMount, useUnmount } from 'mobx-react-use-autorun'
|
|
139
|
+
|
|
140
|
+
export default observer(() => {
|
|
141
|
+
|
|
142
|
+
const state = useMobxState({
|
|
143
|
+
subscription: new Subscription()
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
useMount(() => {
|
|
147
|
+
state.subscription.add(of(null).pipe(
|
|
148
|
+
tap(() => {
|
|
149
|
+
console.log('component is loaded')
|
|
150
|
+
})
|
|
151
|
+
).subscribe())
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
useUnmount(() => {
|
|
155
|
+
state.subscription.unsubscribe()
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
return null;
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
### Get the real data of the proxy object with toJS
|
|
132
162
|
|
|
133
163
|
toJS will cause data to be used, please do not execute toJS(state) in component rendering code, it may cause repeated rendering.<br/>
|
|
134
164
|
Wrong Usage Demonstration:<br/>
|
|
@@ -169,7 +199,7 @@ Correct Example:<br/>
|
|
|
169
199
|
return <button onClick={() => console.log(toJS(state))}>{'Click Me'}</button>;
|
|
170
200
|
})
|
|
171
201
|
|
|
172
|
-
###
|
|
202
|
+
### Define global mutable data with observable
|
|
173
203
|
|
|
174
204
|
import { observable } from 'mobx-react-use-autorun';
|
|
175
205
|
|
|
@@ -195,36 +225,6 @@ Correct Example:<br/>
|
|
|
195
225
|
</div>;
|
|
196
226
|
})
|
|
197
227
|
|
|
198
|
-
## Notes - Lifecycle hooks
|
|
199
|
-
|
|
200
|
-
useMount is executed when the component loaded.<br/>
|
|
201
|
-
useUnmount is executed when the component is unmount.<br/>
|
|
202
|
-
|
|
203
|
-
import { Subscription, of, tap } from 'rxjs'
|
|
204
|
-
import { useMobxState, observer } from 'mobx-react-use-autorun'
|
|
205
|
-
import { useMount, useUnmount } from 'mobx-react-use-autorun'
|
|
206
|
-
|
|
207
|
-
export default observer(() => {
|
|
208
|
-
|
|
209
|
-
const state = useMobxState({
|
|
210
|
-
subscription: new Subscription()
|
|
211
|
-
})
|
|
212
|
-
|
|
213
|
-
useMount(() => {
|
|
214
|
-
state.subscription.add(of(null).pipe(
|
|
215
|
-
tap(() => {
|
|
216
|
-
console.log('component is loaded')
|
|
217
|
-
})
|
|
218
|
-
).subscribe())
|
|
219
|
-
})
|
|
220
|
-
|
|
221
|
-
useUnmount(() => {
|
|
222
|
-
state.subscription.unsubscribe()
|
|
223
|
-
})
|
|
224
|
-
|
|
225
|
-
return null;
|
|
226
|
-
})
|
|
227
|
-
|
|
228
228
|
# Learn More
|
|
229
229
|
|
|
230
230
|
1. React UI framework (https://reactjs.org)<br/>
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mobx_config.js","sourceRoot":"","sources":["../../lib/js/mobx_config.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAEjC,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"mobx_config.js","sourceRoot":"","sources":["../../lib/js/mobx_config.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAEjC,SAAS,CAAC;IACR,cAAc,EAAE,OAAO;CACxB,CAAC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMobxEffect.js","sourceRoot":"","sources":["../../lib/js/useMobxEffect.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;AACrC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAElC,MAAM,CAAC,IAAM,aAAa,GAAG,UAAC,QAAoB,EAAE,cAAsB;
|
|
1
|
+
{"version":3,"file":"useMobxEffect.js","sourceRoot":"","sources":["../../lib/js/useMobxEffect.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ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extendObservable, remove, isObservable, runInAction } from 'mobx';
|
|
2
2
|
import { useLocalObservable } from 'mobx-react-lite';
|
|
3
|
+
import { useRef } from 'react';
|
|
3
4
|
export function useMobxState(state, props) {
|
|
4
5
|
var mobxState = useLocalObservable(typeof state === "function" ? state : function () { return state; });
|
|
6
|
+
var keyListOfState = useRef([]);
|
|
5
7
|
runInAction(function () {
|
|
6
8
|
var _a, _b;
|
|
7
9
|
var _c;
|
|
8
10
|
if (props) {
|
|
11
|
+
for (var _i = 0, _d = keyListOfState.current; _i < _d.length; _i++) {
|
|
12
|
+
var key = _d[_i];
|
|
13
|
+
if (!Object.keys(props).includes(key)) {
|
|
14
|
+
remove(mobxState, key);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
9
17
|
for (var key in props) {
|
|
10
18
|
if (isObservable(props[key]) || ((_c = Object.getOwnPropertyDescriptor(props, key)) === null || _c === void 0 ? void 0 : _c.get)) {
|
|
11
19
|
Object.defineProperty(mobxState, key, Object.getOwnPropertyDescriptor(props, key));
|
|
@@ -17,6 +25,10 @@ export function useMobxState(state, props) {
|
|
|
17
25
|
}
|
|
18
26
|
}
|
|
19
27
|
}
|
|
28
|
+
keyListOfState.current.splice(0, keyListOfState.current.length);
|
|
29
|
+
for (var key in props) {
|
|
30
|
+
keyListOfState.current.push(key);
|
|
31
|
+
}
|
|
20
32
|
}
|
|
21
33
|
});
|
|
22
34
|
return mobxState;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMobxState.js","sourceRoot":"","sources":["../../lib/js/useMobxState.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,MAAM,CAAC;AAC3E,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"useMobxState.js","sourceRoot":"","sources":["../../lib/js/useMobxState.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAg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,GAAG,CAAC,iBAAM,GAAC,GAAG,IAAG,KAAK,MAAG,CAAA;qBACrE;iBACF;aACF;YAED,cAAc,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,cAAc,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YAChE,KAAK,IAAM,GAAG,IAAI,KAAK,EAAE;gBACvB,cAAc,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;aAClC;SACF;IACH,CAAC,CAAC,CAAA;IAEF,OAAO,SAAgB,CAAC;AAC1B,CAAC"}
|
package/lib/js/mobx_config.tsx
CHANGED
package/lib/js/useMobxEffect.tsx
CHANGED
|
@@ -6,29 +6,29 @@ import { useEffect } from 'react';
|
|
|
6
6
|
|
|
7
7
|
export const useMobxEffect = (callback: () => void, dependencyList?: any[]): void => {
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
const state = useMobxState({
|
|
10
|
+
subscription: new Subscription(),
|
|
11
|
+
}, { callback })
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
const source = useMobxState({}, Object.assign({}, dependencyList));
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
if (!dependencyList) {
|
|
17
|
+
callback()
|
|
18
|
+
}
|
|
19
|
+
})
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
useMount(() => {
|
|
22
|
+
if (dependencyList) {
|
|
23
|
+
const disposer = reaction(() => [toJS(source)], () => state.callback(), { fireImmediately: true, delay: 1 });
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
state.subscription.add(new Subscription(() => {
|
|
26
|
+
disposer();
|
|
27
|
+
}));
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
useUnmount(() => {
|
|
32
|
+
state.subscription.unsubscribe();
|
|
33
|
+
})
|
|
34
34
|
}
|
package/lib/js/useMobxState.tsx
CHANGED
|
@@ -1,27 +1,42 @@
|
|
|
1
1
|
import { extendObservable, remove, isObservable, runInAction } from 'mobx';
|
|
2
2
|
import { useLocalObservable } from 'mobx-react-lite';
|
|
3
|
+
import { useRef } from 'react';
|
|
3
4
|
|
|
4
5
|
export function useMobxState<T extends Record<any, any>>(state: T | (() => T)): T;
|
|
5
6
|
export function useMobxState<T extends Record<any, any>, P extends Record<any, any>>(state: T | (() => T), props: P): T & P;
|
|
6
7
|
|
|
7
8
|
export function useMobxState<T extends Record<any, any>, P extends Record<any, any>>(state: T | (() => T), props?: P): T & P {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
extendObservable(mobxState, { [key]: props[key] }, { [key]: false })
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
9
|
+
const mobxState = useLocalObservable(typeof state === "function" ? (state as any) : () => state);
|
|
10
|
+
|
|
11
|
+
const keyListOfState = useRef<string[]>([]);
|
|
12
|
+
|
|
13
|
+
runInAction(() => {
|
|
14
|
+
|
|
15
|
+
if (props) {
|
|
16
|
+
|
|
17
|
+
for (const key of keyListOfState.current) {
|
|
18
|
+
if (!Object.keys(props).includes(key)) {
|
|
19
|
+
remove(mobxState, key);
|
|
23
20
|
}
|
|
24
|
-
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
for (const key in props) {
|
|
24
|
+
if (isObservable(props[key]) || Object.getOwnPropertyDescriptor(props, key)?.get) {
|
|
25
|
+
Object.defineProperty(mobxState, key, Object.getOwnPropertyDescriptor(props, key) as any)
|
|
26
|
+
} else {
|
|
27
|
+
if (props[key] !== mobxState[key]) {
|
|
28
|
+
remove(mobxState, key);
|
|
29
|
+
extendObservable(mobxState, { [key]: props[key] }, { [key]: false })
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
keyListOfState.current.splice(0, keyListOfState.current.length);
|
|
35
|
+
for (const key in props) {
|
|
36
|
+
keyListOfState.current.push(key);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
})
|
|
25
40
|
|
|
26
|
-
|
|
41
|
+
return mobxState as any;
|
|
27
42
|
}
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"mobx-react-use-autorun","version":"3.1.
|
|
1
|
+
{"name":"mobx-react-use-autorun","version":"3.1.9","description":"React Hook for mobx","scripts":{"test":"npx -y -p typescript -p ts-node ts-node --skipProject bin/mobx_react_test.ts","build":"npx -y -p typescript -p ts-node ts-node --skipProject bin/mobx_react_build.ts","make":"npx -y -p typescript -p ts-node ts-node --skipProject bin/mobx_react_make.ts"},"dependencies":{"mobx":"6.8.0","mobx-react-lite":"3.4.0","react-use":"17.4.0","rxjs":"7.8.0"},"peerDependencies":{"react":">=16.8.0"},"license":"MIT","keywords":["react","mobx","typescript"],"main":"./dist/index.js","types":"./dist/index.d.ts","eslintConfig":{"extends":["react-app","react-app/jest"]},"homepage":"https://github.com/zdu-strong/mobx-react-use-autorun#readme","repository":{"type":"git","url":"git+https://github.com/zdu-strong/mobx-react-use-autorun.git"},"files":["lib","dist"],"bugs":{"url":"https://github.com/zdu-strong/mobx-react-use-autorun/issues"},"publishConfig":{"registry":"https://registry.npmjs.org/"}}
|