mobx-react-use-autorun 3.0.22 → 3.0.25
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 +77 -9
- package/dist/lib/useMobxEffect.d.ts +1 -1
- package/package.json +1 -61
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
|
|
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.
|
|
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,19 @@ Form validation<br/>
|
|
|
72
72
|
</div>)
|
|
73
73
|
})
|
|
74
74
|
|
|
75
|
+
useMobxState provides two usages.<br/>
|
|
76
|
+
|
|
77
|
+
useMobxState({},{}) is easy to use, you can define state and third-party hooks.<br/>
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
useMobxState(()=>({
|
|
81
|
+
get (){
|
|
82
|
+
return state.name
|
|
83
|
+
}
|
|
84
|
+
}),(props: {name: string})=>({
|
|
85
|
+
...props
|
|
86
|
+
})) provides advanced usage, the state is executed only once, and the performance is better. At the same time, you can use the get computed property to recalculate when the computed value changes.<br/>
|
|
87
|
+
|
|
75
88
|
## Notes - Subscription property changes with useMobxEffect
|
|
76
89
|
|
|
77
90
|
import { useMobxState, observer, useMobxEffect, toJS } from 'mobx-react-use-autorun';
|
|
@@ -82,25 +95,80 @@ Form validation<br/>
|
|
|
82
95
|
|
|
83
96
|
useMobxEffect(() => {
|
|
84
97
|
console.log(toJS(state))
|
|
85
|
-
}, [state])
|
|
98
|
+
}, [state.randomNumber])
|
|
86
99
|
|
|
87
100
|
return <div onClick={() => state.randomNumber = Math.random()}>
|
|
88
101
|
{state.randomNumber}
|
|
89
102
|
</div>
|
|
90
103
|
})
|
|
91
104
|
|
|
105
|
+
## Notes - Get the real data of the proxy object with toJS
|
|
106
|
+
|
|
107
|
+
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:
|
|
108
|
+
|
|
109
|
+
import { toJS, observer, useMobxState } from 'mobx-react-use-autorun'
|
|
110
|
+
import { v1 } from 'uuid'
|
|
111
|
+
|
|
112
|
+
export default observer(() => {
|
|
113
|
+
|
|
114
|
+
const state = useMobxState({}, {
|
|
115
|
+
id: v1()
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
toJS(state)
|
|
119
|
+
|
|
120
|
+
return null;
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
Other than that, all usages are correct. Example:
|
|
124
|
+
|
|
125
|
+
import { toJS, useMobxEffect, observer, useMobxState } from 'mobx-react-use-autorun';
|
|
126
|
+
import { v1 } from 'uuid'
|
|
127
|
+
|
|
128
|
+
export default observer(() => {
|
|
129
|
+
|
|
130
|
+
const state = useMobxState({}, {
|
|
131
|
+
name: v1()
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
useMobxEffect(() => {
|
|
135
|
+
console.log(toJS(state))
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
console.log(toJS(state.name))
|
|
139
|
+
|
|
140
|
+
return <button onClick={() => console.log(toJS(state))}>{'Click Me'}</button>;
|
|
141
|
+
})
|
|
142
|
+
|
|
92
143
|
## Notes - Define global mutable data
|
|
93
144
|
|
|
94
145
|
import { observable } from 'mobx-react-use-autorun';
|
|
95
146
|
|
|
96
147
|
const state = observable({});
|
|
97
148
|
|
|
98
|
-
##
|
|
149
|
+
## Introduction to third-party hooks
|
|
99
150
|
|
|
100
|
-
|
|
151
|
+
useMount is executed when the component loaded.
|
|
152
|
+
useUnmount is executed when the component is unmount.
|
|
101
153
|
|
|
102
|
-
|
|
103
|
-
|
|
154
|
+
import { useMount, useUnmount } from 'react-use'
|
|
155
|
+
import { Subscription, of, tap } from 'rxjs'
|
|
156
|
+
|
|
157
|
+
const state = useMobxState({
|
|
158
|
+
subscription: new Subscription()
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
useMount(() => {
|
|
162
|
+
state.subscription.add(of(null).pipe(
|
|
163
|
+
tap(() => {
|
|
164
|
+
console.log('component is loaded')
|
|
165
|
+
})
|
|
166
|
+
).subscribe())
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
useUnmount(() => {
|
|
170
|
+
state.subscription.unsubscribe()
|
|
171
|
+
})
|
|
104
172
|
|
|
105
173
|
## Learn More
|
|
106
174
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const useMobxEffect: (callback: () => void, dependencyList?: any[]
|
|
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.25","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/"}}
|