mobx-react-use-autorun 3.0.22 → 3.0.24

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 CHANGED
@@ -4,7 +4,7 @@ This project was bootstrapped with [Create React App](https://github.com/faceboo
4
4
 
5
5
  ## Development environment setup
6
6
  1. From https://code.visualstudio.com install Visual Studio Code.<br/>
7
- 2. From https://nodejs.org/en/ install nodejs v16.<br/>
7
+ 2. From https://nodejs.org install nodejs v16.<br/>
8
8
 
9
9
  ## Available Scripts
10
10
 
@@ -44,7 +44,7 @@ Form validation<br/>
44
44
 
45
45
  export default observer(() => {
46
46
 
47
- const state = useMobxState({
47
+ const state = useMobxState(() => ({
48
48
  name: "",
49
49
  submit: false,
50
50
  errors: {
@@ -52,10 +52,10 @@ Form validation<br/>
52
52
  return state.submit && !state.name && "请填写名称";
53
53
  },
54
54
  get hasError() {
55
- return state.errors.name;
55
+ return Object.keys(state.errors).filter(s => s !== "hasError").some(s => (state.errors as any)[s]);
56
56
  }
57
57
  }
58
- });
58
+ }));
59
59
 
60
60
  const ok = async () => {
61
61
  state.submit = true;
@@ -72,6 +72,18 @@ Form validation<br/>
72
72
  </div>)
73
73
  })
74
74
 
75
+ useMobxState提供两种用法.
76
+
77
+ useMobxState({},{})使用简单, 可以定义state和第三方hooks.
78
+
79
+ useMobxState(()=>({
80
+ get (){
81
+ return state.name
82
+ }
83
+ }),(props: {name: string})=>({
84
+ ...props
85
+ })) 提供高级用法, state只执行一次, 性能更好, 同时可以使用get计算属性, 在计算值变化时, 重新计算.
86
+
75
87
  ## Notes - Subscription property changes with useMobxEffect
76
88
 
77
89
  import { useMobxState, observer, useMobxEffect, toJS } from 'mobx-react-use-autorun';
@@ -82,25 +94,80 @@ Form validation<br/>
82
94
 
83
95
  useMobxEffect(() => {
84
96
  console.log(toJS(state))
85
- }, [state]);
97
+ }, [state.randomNumber])
86
98
 
87
99
  return <div onClick={() => state.randomNumber = Math.random()}>
88
100
  {state.randomNumber}
89
101
  </div>
90
102
  })
91
103
 
104
+ ## Notes - Get the real data of the proxy object with toJS
105
+
106
+ toJS will cause data to be used, Please do not execute toJS(state) in component rendering code, it may cause repeated rendering. Wrong Usage Demonstration:
107
+
108
+ import { toJS, observer, useMobxState } from 'mobx-react-use-autorun'
109
+ import { v1 } from 'uuid'
110
+
111
+ export default observer(() => {
112
+
113
+ const state = useMobxState({}, {
114
+ id: v1()
115
+ })
116
+
117
+ toJS(state)
118
+
119
+ return null;
120
+ })
121
+
122
+ Other than that, all usages are correct. Example:
123
+
124
+ import { toJS, useMobxEffect, observer, useMobxState } from 'mobx-react-use-autorun';
125
+ import { v1 } from 'uuid'
126
+
127
+ export default observer(() => {
128
+
129
+ const state = useMobxState({}, {
130
+ name: v1()
131
+ });
132
+
133
+ useMobxEffect(() => {
134
+ console.log(toJS(state))
135
+ })
136
+
137
+ console.log(toJS(state.name))
138
+
139
+ return <button onClick={() => console.log(toJS(state))}>{'Click Me'}</button>;
140
+ })
141
+
92
142
  ## Notes - Define global mutable data
93
143
 
94
144
  import { observable } from 'mobx-react-use-autorun';
95
145
 
96
146
  const state = observable({});
97
147
 
98
- ## Notes - Get the real data of the proxy object with toJS
148
+ ## Introduction to third-party hooks
99
149
 
100
- import { observable, toJS } from 'mobx-react-use-autorun';
150
+ useMount is executed when the component loaded.
151
+ useUnmount is executed when the component is unmount.
101
152
 
102
- const state = observable({});
103
- console.log(toJS(state));
153
+ import { useMount, useUnmount } from 'react-use'
154
+ import { Subscription, of, tap } from 'rxjs'
155
+
156
+ const state = useMobxState({
157
+ subscription: new Subscription()
158
+ })
159
+
160
+ useMount(() => {
161
+ state.subscription.add(of(null).pipe(
162
+ tap(() => {
163
+ console.log('component is loaded')
164
+ })
165
+ ).subscribe())
166
+ })
167
+
168
+ useUnmount(() => {
169
+ state.subscription.unsubscribe()
170
+ })
104
171
 
105
172
  ## Learn More
106
173
 
@@ -1 +1 @@
1
- export declare const useMobxEffect: (callback: () => void, dependencyList?: any[] | undefined) => void;
1
+ export declare const useMobxEffect: (callback: () => void, dependencyList?: any[]) => void;
package/package.json CHANGED
@@ -1,61 +1 @@
1
- {
2
- "name": "mobx-react-use-autorun",
3
- "version": "3.0.22",
4
- "description": "React Hook for mobx",
5
- "scripts": {
6
- "test": "npm install -g typescript@latest @types/node@latest ts-node@latest cross-env@latest && cross-env TS_NODE_SKIP_PROJECT=true ts-node bin/mobx_react_test.ts",
7
- "build": "npm install -g typescript@latest @types/node@latest ts-node@latest cross-env@latest && cross-env TS_NODE_SKIP_PROJECT=true ts-node bin/mobx_react_build.ts",
8
- "make": "npm install -g typescript@latest @types/node@latest ts-node@latest cross-env@latest && cross-env TS_NODE_SKIP_PROJECT=true ts-node bin/mobx_react_make.ts"
9
- },
10
- "dependencies": {
11
- "mobx": ">=6.5.0",
12
- "mobx-react-lite": ">=3.3.0",
13
- "react-use": ">=17.3.2",
14
- "rxjs": ">=6.0.0"
15
- },
16
- "peerDependencies": {
17
- "react": ">=16.8.0"
18
- },
19
- "devDependencies": {
20
- "@testing-library/jest-dom": "5.16.5",
21
- "@testing-library/react": "13.4.0",
22
- "@testing-library/user-event": "14.4.3",
23
- "@types/jest": "27.5.0",
24
- "@types/node": "18.7.15",
25
- "@types/react": "18.0.18",
26
- "@types/react-dom": "18.0.6",
27
- "react": "18.2.0",
28
- "react-dom": "18.2.0",
29
- "react-scripts": "5.0.1",
30
- "typescript": "4.6.4"
31
- },
32
- "license": "MIT",
33
- "keywords": [
34
- "react",
35
- "mobx",
36
- "typescript"
37
- ],
38
- "main": "./dist/index.js",
39
- "types": "./dist/index.d.ts",
40
- "eslintConfig": {
41
- "extends": [
42
- "react-app",
43
- "react-app/jest"
44
- ]
45
- },
46
- "homepage": "https://github.com/zdu-strong/mobx-react-use-autorun#readme",
47
- "repository": {
48
- "type": "git",
49
- "url": "git+https://github.com/zdu-strong/mobx-react-use-autorun.git"
50
- },
51
- "files": [
52
- "lib",
53
- "dist"
54
- ],
55
- "bugs": {
56
- "url": "https://github.com/zdu-strong/mobx-react-use-autorun/issues"
57
- },
58
- "publishConfig": {
59
- "registry": "https://registry.npmjs.org/"
60
- }
61
- }
1
+ {"name":"mobx-react-use-autorun","version":"3.0.24","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.5.0","mobx-react-lite":">=3.3.0","react-use":">=17.3.2","rxjs":">=6.0.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/"}}