mobx-react-use-autorun 4.0.52 → 4.0.53
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/LICENSE +24 -24
- package/README.md +315 -315
- package/dist/cjs/index.d.ts +9 -9
- package/dist/cjs/index.js +16 -16
- package/dist/cjs/js/mobx_config.d.ts +1 -1
- package/dist/cjs/js/mobx_config.js +6 -6
- package/dist/cjs/js/useMobxEffect.d.ts +1 -1
- package/dist/cjs/js/useMobxEffect.js +47 -47
- package/dist/cjs/js/useMobxState.d.ts +2 -2
- package/dist/cjs/js/useMobxState.js +31 -31
- package/dist/cjs/js/useMount.d.ts +2 -2
- package/dist/cjs/js/useMount.js +23 -23
- package/dist/esm/index.d.ts +9 -9
- package/dist/esm/index.js +9 -9
- package/dist/esm/js/mobx_config.d.ts +1 -1
- package/dist/esm/js/mobx_config.js +4 -4
- package/dist/esm/js/useMobxEffect.d.ts +1 -1
- package/dist/esm/js/useMobxEffect.js +43 -43
- package/dist/esm/js/useMobxState.d.ts +2 -2
- package/dist/esm/js/useMobxState.js +27 -27
- package/dist/esm/js/useMount.d.ts +2 -2
- package/dist/esm/js/useMount.js +19 -19
- package/lib/index.tsx +9 -9
- package/lib/js/mobx_config.tsx +4 -4
- package/lib/js/useMobxEffect.tsx +56 -56
- package/lib/js/useMobxState.tsx +35 -35
- package/lib/js/useMount.tsx +23 -23
- package/package.json +3 -3
package/LICENSE
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
## MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 zdu https://github.com/zdu-strong/mobx-react-use-autorun
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person
|
|
6
|
-
obtaining a copy of this software and associated documentation
|
|
7
|
-
files (the "Software"), to deal in the Software without
|
|
8
|
-
restriction, including without limitation the rights to use,
|
|
9
|
-
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
-
copies of the Software, and to permit persons to whom the
|
|
11
|
-
Software is furnished to do so, subject to the following
|
|
12
|
-
conditions:
|
|
13
|
-
|
|
14
|
-
The above copyright notice and this permission notice shall be
|
|
15
|
-
included in all copies or substantial portions of the Software.
|
|
16
|
-
|
|
17
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
18
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
19
|
-
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
20
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
21
|
-
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
22
|
-
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
23
|
-
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
24
|
-
OTHER DEALINGS IN THE SOFTWARE.
|
|
1
|
+
## MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 zdu https://github.com/zdu-strong/mobx-react-use-autorun
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person
|
|
6
|
+
obtaining a copy of this software and associated documentation
|
|
7
|
+
files (the "Software"), to deal in the Software without
|
|
8
|
+
restriction, including without limitation the rights to use,
|
|
9
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the
|
|
11
|
+
Software is furnished to do so, subject to the following
|
|
12
|
+
conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be
|
|
15
|
+
included in all copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
19
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
20
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
21
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
22
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
23
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
24
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,316 +1,316 @@
|
|
|
1
|
-
Provide concise usage for mobx in react<br/>
|
|
2
|
-
|
|
3
|
-
# `Installation`
|
|
4
|
-
|
|
5
|
-
npm install mobx-react-use-autorun
|
|
6
|
-
|
|
7
|
-
# `Usage`
|
|
8
|
-
|
|
9
|
-
### Define state with useMobxState
|
|
10
|
-
|
|
11
|
-
import { observer, useMobxState } from 'mobx-react-use-autorun';
|
|
12
|
-
|
|
13
|
-
export default observer(() => {
|
|
14
|
-
|
|
15
|
-
const state = useMobxState({
|
|
16
|
-
age: 16
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
return <div
|
|
20
|
-
onClick={() => state.age++}
|
|
21
|
-
>
|
|
22
|
-
{`John's age is ${state.age}`}
|
|
23
|
-
</div>
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
More example - Form validation:<br/>
|
|
27
|
-
|
|
28
|
-
import { Button, TextField } from '@mui/material';
|
|
29
|
-
import { observer, useMobxState } from 'mobx-react-use-autorun';
|
|
30
|
-
|
|
31
|
-
export default observer(() => {
|
|
32
|
-
|
|
33
|
-
const state = useMobxState({
|
|
34
|
-
name: "",
|
|
35
|
-
submit: false,
|
|
36
|
-
errors: {
|
|
37
|
-
name() {
|
|
38
|
-
return state.submit &&
|
|
39
|
-
!state.name &&
|
|
40
|
-
"Please fill in the username";
|
|
41
|
-
},
|
|
42
|
-
hasError() {
|
|
43
|
-
return Object.keys(state.errors)
|
|
44
|
-
.filter(s => s !== "hasError")
|
|
45
|
-
.some(s => (state.errors as any)[s]());
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
async function ok(){
|
|
51
|
-
state.submit = true;
|
|
52
|
-
if (state.errors.hasError()) {
|
|
53
|
-
console.log("Submission Failed");
|
|
54
|
-
} else {
|
|
55
|
-
console.log("Submitted successfully");
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return (<div className='flex flex-col' style={{ padding: "2em" }}>
|
|
60
|
-
<TextField
|
|
61
|
-
value={state.name}
|
|
62
|
-
label="Username"
|
|
63
|
-
onChange={(e) => state.name = e.target.value}
|
|
64
|
-
error={!!state.errors.name()}
|
|
65
|
-
helperText={state.errors.name()}
|
|
66
|
-
/>
|
|
67
|
-
<Button
|
|
68
|
-
variant="contained"
|
|
69
|
-
style={{ marginTop: "2em" }}
|
|
70
|
-
onClick={ok}
|
|
71
|
-
>Submit</Button>
|
|
72
|
-
</div>)
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
More example - Use props and other hooks:<br/>
|
|
76
|
-
|
|
77
|
-
import { observer, useMobxState } from 'mobx-react-use-autorun';
|
|
78
|
-
import { useRef } from 'react';
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
export default observer((props: { user: { name: string, age: number } }) => {
|
|
82
|
-
|
|
83
|
-
const state = useMobxState({
|
|
84
|
-
}, {
|
|
85
|
-
containerRef: useRef<HTMLDivElement>(null),
|
|
86
|
-
...props,
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
return <div
|
|
90
|
-
ref={state.containerRef}
|
|
91
|
-
onClick={() => {
|
|
92
|
-
props.user.age++;
|
|
93
|
-
}}
|
|
94
|
-
>
|
|
95
|
-
{`${props.user.name}'s age is ${props.user.age}.`}
|
|
96
|
-
</div>
|
|
97
|
-
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
### Lifecycle hook with useMount
|
|
101
|
-
|
|
102
|
-
useMount is a lifecycle hook that calls a function after the component is mounted.<br/>
|
|
103
|
-
It provides a subscription as parameter, which will be unsubscribed when the component will unmount.<br/>
|
|
104
|
-
|
|
105
|
-
It support Strict Mode.<br/>
|
|
106
|
-
Strict Mode: In the future, React will provide a feature that lets components preserve state between unmounts. To prepare for it, React 18 introduces a new development-only check to Strict Mode. React will automatically unmount and remount every component, whenever a component mounts for the first time, restoring the previous state on the second mount. If this breaks your app, consider removing Strict Mode until you can fix the components to be resilient to remounting with existing state.<br/>
|
|
107
|
-
|
|
108
|
-
import { Subscription } from 'rxjs'
|
|
109
|
-
import { observer, useMount } from 'mobx-react-use-autorun'
|
|
110
|
-
|
|
111
|
-
export default observer(() => {
|
|
112
|
-
|
|
113
|
-
useMount((subscription) => {
|
|
114
|
-
console.log('component is loaded')
|
|
115
|
-
|
|
116
|
-
subscription.add(new Subscription(() => {
|
|
117
|
-
console.log("component will unmount")
|
|
118
|
-
}))
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
return null;
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
### Subscription property changes with useMobxEffect
|
|
125
|
-
|
|
126
|
-
import { useMobxState, observer } from 'mobx-react-use-autorun';
|
|
127
|
-
import { useMobxEffect, toJS } from 'mobx-react-use-autorun'
|
|
128
|
-
|
|
129
|
-
export default observer(() => {
|
|
130
|
-
|
|
131
|
-
const state = useMobxState({
|
|
132
|
-
randomNumber: 1
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
useMobxEffect(() => {
|
|
136
|
-
console.log(toJS(state))
|
|
137
|
-
}, [state.randomNumber])
|
|
138
|
-
|
|
139
|
-
return <div onClick={() => state.randomNumber = Math.random()}>
|
|
140
|
-
{state.randomNumber}
|
|
141
|
-
</div>
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
### Define global mutable data with observable
|
|
145
|
-
|
|
146
|
-
// File: GlobalState.tsx
|
|
147
|
-
import { observable } from 'mobx-react-use-autorun';
|
|
148
|
-
|
|
149
|
-
export const globalState = observable({
|
|
150
|
-
age: 15,
|
|
151
|
-
name: 'tom'
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
// File: UserComponent.tsx
|
|
155
|
-
import { observer } from "mobx-react-use-autorun";
|
|
156
|
-
import { globalState } from "./GlobalState";
|
|
157
|
-
|
|
158
|
-
export default observer(() => {
|
|
159
|
-
return <div
|
|
160
|
-
onClick={() => {
|
|
161
|
-
globalState.age++;
|
|
162
|
-
}}
|
|
163
|
-
>
|
|
164
|
-
{`${globalState.name}'s age is ${globalState.age}.`}
|
|
165
|
-
</div>;
|
|
166
|
-
})
|
|
167
|
-
|
|
168
|
-
### Get the real data of the proxy object with toJS
|
|
169
|
-
|
|
170
|
-
"toJS" will cause the component to re-render when data changes.<br/>
|
|
171
|
-
Please do not execute "toJS(state)" in component rendering code, it may cause repeated rendering.<br/>
|
|
172
|
-
Wrong Example:<br/>
|
|
173
|
-
|
|
174
|
-
import { toJS, observer, useMobxState } from 'mobx-react-use-autorun'
|
|
175
|
-
import { v1 } from 'uuid'
|
|
176
|
-
|
|
177
|
-
export default observer(() => {
|
|
178
|
-
|
|
179
|
-
const state = useMobxState({}, {
|
|
180
|
-
id: v1()
|
|
181
|
-
})
|
|
182
|
-
|
|
183
|
-
toJS(state)
|
|
184
|
-
|
|
185
|
-
return null;
|
|
186
|
-
})
|
|
187
|
-
|
|
188
|
-
Other than that, all usages are correct.<br/>
|
|
189
|
-
Correct Example:<br/>
|
|
190
|
-
|
|
191
|
-
import { toJS, useMobxEffect } from 'mobx-react-use-autorun';
|
|
192
|
-
import { observer, useMobxState } from 'mobx-react-use-autorun';
|
|
193
|
-
import { v1 } from 'uuid'
|
|
194
|
-
|
|
195
|
-
export default observer(() => {
|
|
196
|
-
|
|
197
|
-
const state = useMobxState({
|
|
198
|
-
name: v1()
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
useMobxEffect(() => {
|
|
202
|
-
console.log(toJS(state));
|
|
203
|
-
console.log(toJS(state.name));
|
|
204
|
-
})
|
|
205
|
-
|
|
206
|
-
console.log(toJS(state.name));
|
|
207
|
-
|
|
208
|
-
return <button
|
|
209
|
-
onClick={() => {
|
|
210
|
-
console.log(toJS(state));
|
|
211
|
-
console.log(toJS(state.name));
|
|
212
|
-
}}
|
|
213
|
-
>
|
|
214
|
-
{'Click Me'}
|
|
215
|
-
</button>;
|
|
216
|
-
})
|
|
217
|
-
|
|
218
|
-
# Notes - Work with non-observer components
|
|
219
|
-
|
|
220
|
-
Non-observer components cannot trigger re-rendering when the following data changes, please use "toJS" to do it.<br/>
|
|
221
|
-
* array<br/>
|
|
222
|
-
* object<br/>
|
|
223
|
-
* The all data used in the new render callback<br/>
|
|
224
|
-
|
|
225
|
-
<br/>
|
|
226
|
-
|
|
227
|
-
import { observer, toJS, useMobxState } from "mobx-react-use-autorun";
|
|
228
|
-
import { v1 } from "uuid";
|
|
229
|
-
import { Virtuoso } from 'react-virtuoso'
|
|
230
|
-
|
|
231
|
-
export default observer(() => {
|
|
232
|
-
|
|
233
|
-
const state = useMobxState({
|
|
234
|
-
userList: [{ id: 1, username: 'Tom' }]
|
|
235
|
-
})
|
|
236
|
-
|
|
237
|
-
toJS(state.userList)
|
|
238
|
-
|
|
239
|
-
return <Virtuoso
|
|
240
|
-
style={{ width: "500px", height: "200px" }}
|
|
241
|
-
data={state.userList}
|
|
242
|
-
itemContent={(index, item) =>
|
|
243
|
-
<div key={item.id} onClick={() => item.username = v1()}>
|
|
244
|
-
{item.username}
|
|
245
|
-
</div>
|
|
246
|
-
}
|
|
247
|
-
/>
|
|
248
|
-
})
|
|
249
|
-
|
|
250
|
-
# Notes - Work with typedjson
|
|
251
|
-
|
|
252
|
-
Typedjson is a strongly typed reflection library.<br/>
|
|
253
|
-
|
|
254
|
-
import { makeAutoObservable, toJS } from "mobx-react-use-autorun";
|
|
255
|
-
import { TypedJSON, jsonMember, jsonObject } from "typedjson";
|
|
256
|
-
|
|
257
|
-
@jsonObject
|
|
258
|
-
export class UserModel {
|
|
259
|
-
|
|
260
|
-
@jsonMember(String)
|
|
261
|
-
username!: string;
|
|
262
|
-
|
|
263
|
-
@jsonMember(Date)
|
|
264
|
-
createDate!: Date;
|
|
265
|
-
|
|
266
|
-
constructor() {
|
|
267
|
-
makeAutoObservable(this);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
const user = new TypedJSON(UserModel).parse(`{"username":"tom","createDate":"2023-04-13T04:21:59.262Z"}`);
|
|
272
|
-
console.log(toJS(user));
|
|
273
|
-
|
|
274
|
-
const anotherUser = new TypedJSON(UserModel).parse({
|
|
275
|
-
username: "tom",
|
|
276
|
-
createDate: "2023-04-13T04:21:59.262Z"
|
|
277
|
-
});
|
|
278
|
-
console.log(toJS(anotherUser));
|
|
279
|
-
|
|
280
|
-
# Learn More
|
|
281
|
-
|
|
282
|
-
1. A JavaScript library for building user interfaces (https://react.dev)<br/>
|
|
283
|
-
2. Reactive Extensions Library for JavaScript (https://www.npmjs.com/package/rxjs)
|
|
284
|
-
3. Material UI is a library of React UI components that implements Google's Material Design (https://mui.com)
|
|
285
|
-
|
|
286
|
-
# Getting Started
|
|
287
|
-
|
|
288
|
-
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). If you have any questions, please contact zdu.strong@gmail.com.<br/>
|
|
289
|
-
|
|
290
|
-
## Development environment setup
|
|
291
|
-
1. From https://code.visualstudio.com install Visual Studio Code.<br/>
|
|
292
|
-
2. From https://nodejs.org install nodejs v22.<br/>
|
|
293
|
-
|
|
294
|
-
## Available Scripts
|
|
295
|
-
|
|
296
|
-
In the project directory, you can run:<br/>
|
|
297
|
-
|
|
298
|
-
### `npm test`
|
|
299
|
-
|
|
300
|
-
Run all unit tests.<br/>
|
|
301
|
-
|
|
302
|
-
### `npm run build`
|
|
303
|
-
|
|
304
|
-
Builds the files for production to the `dist` folder.<br/>
|
|
305
|
-
|
|
306
|
-
### `npm run make`
|
|
307
|
-
|
|
308
|
-
Publish to npm repository<br/>
|
|
309
|
-
|
|
310
|
-
Pre-step, please run<br/>
|
|
311
|
-
|
|
312
|
-
npm login --registry https://registry.npmjs.org
|
|
313
|
-
|
|
314
|
-
# License
|
|
315
|
-
|
|
1
|
+
Provide concise usage for mobx in react<br/>
|
|
2
|
+
|
|
3
|
+
# `Installation`
|
|
4
|
+
|
|
5
|
+
npm install mobx-react-use-autorun
|
|
6
|
+
|
|
7
|
+
# `Usage`
|
|
8
|
+
|
|
9
|
+
### Define state with useMobxState
|
|
10
|
+
|
|
11
|
+
import { observer, useMobxState } from 'mobx-react-use-autorun';
|
|
12
|
+
|
|
13
|
+
export default observer(() => {
|
|
14
|
+
|
|
15
|
+
const state = useMobxState({
|
|
16
|
+
age: 16
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
return <div
|
|
20
|
+
onClick={() => state.age++}
|
|
21
|
+
>
|
|
22
|
+
{`John's age is ${state.age}`}
|
|
23
|
+
</div>
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
More example - Form validation:<br/>
|
|
27
|
+
|
|
28
|
+
import { Button, TextField } from '@mui/material';
|
|
29
|
+
import { observer, useMobxState } from 'mobx-react-use-autorun';
|
|
30
|
+
|
|
31
|
+
export default observer(() => {
|
|
32
|
+
|
|
33
|
+
const state = useMobxState({
|
|
34
|
+
name: "",
|
|
35
|
+
submit: false,
|
|
36
|
+
errors: {
|
|
37
|
+
name() {
|
|
38
|
+
return state.submit &&
|
|
39
|
+
!state.name &&
|
|
40
|
+
"Please fill in the username";
|
|
41
|
+
},
|
|
42
|
+
hasError() {
|
|
43
|
+
return Object.keys(state.errors)
|
|
44
|
+
.filter(s => s !== "hasError")
|
|
45
|
+
.some(s => (state.errors as any)[s]());
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
async function ok(){
|
|
51
|
+
state.submit = true;
|
|
52
|
+
if (state.errors.hasError()) {
|
|
53
|
+
console.log("Submission Failed");
|
|
54
|
+
} else {
|
|
55
|
+
console.log("Submitted successfully");
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return (<div className='flex flex-col' style={{ padding: "2em" }}>
|
|
60
|
+
<TextField
|
|
61
|
+
value={state.name}
|
|
62
|
+
label="Username"
|
|
63
|
+
onChange={(e) => state.name = e.target.value}
|
|
64
|
+
error={!!state.errors.name()}
|
|
65
|
+
helperText={state.errors.name()}
|
|
66
|
+
/>
|
|
67
|
+
<Button
|
|
68
|
+
variant="contained"
|
|
69
|
+
style={{ marginTop: "2em" }}
|
|
70
|
+
onClick={ok}
|
|
71
|
+
>Submit</Button>
|
|
72
|
+
</div>)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
More example - Use props and other hooks:<br/>
|
|
76
|
+
|
|
77
|
+
import { observer, useMobxState } from 'mobx-react-use-autorun';
|
|
78
|
+
import { useRef } from 'react';
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
export default observer((props: { user: { name: string, age: number } }) => {
|
|
82
|
+
|
|
83
|
+
const state = useMobxState({
|
|
84
|
+
}, {
|
|
85
|
+
containerRef: useRef<HTMLDivElement>(null),
|
|
86
|
+
...props,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
return <div
|
|
90
|
+
ref={state.containerRef}
|
|
91
|
+
onClick={() => {
|
|
92
|
+
props.user.age++;
|
|
93
|
+
}}
|
|
94
|
+
>
|
|
95
|
+
{`${props.user.name}'s age is ${props.user.age}.`}
|
|
96
|
+
</div>
|
|
97
|
+
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
### Lifecycle hook with useMount
|
|
101
|
+
|
|
102
|
+
useMount is a lifecycle hook that calls a function after the component is mounted.<br/>
|
|
103
|
+
It provides a subscription as parameter, which will be unsubscribed when the component will unmount.<br/>
|
|
104
|
+
|
|
105
|
+
It support Strict Mode.<br/>
|
|
106
|
+
Strict Mode: In the future, React will provide a feature that lets components preserve state between unmounts. To prepare for it, React 18 introduces a new development-only check to Strict Mode. React will automatically unmount and remount every component, whenever a component mounts for the first time, restoring the previous state on the second mount. If this breaks your app, consider removing Strict Mode until you can fix the components to be resilient to remounting with existing state.<br/>
|
|
107
|
+
|
|
108
|
+
import { Subscription } from 'rxjs'
|
|
109
|
+
import { observer, useMount } from 'mobx-react-use-autorun'
|
|
110
|
+
|
|
111
|
+
export default observer(() => {
|
|
112
|
+
|
|
113
|
+
useMount((subscription) => {
|
|
114
|
+
console.log('component is loaded')
|
|
115
|
+
|
|
116
|
+
subscription.add(new Subscription(() => {
|
|
117
|
+
console.log("component will unmount")
|
|
118
|
+
}))
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
return null;
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
### Subscription property changes with useMobxEffect
|
|
125
|
+
|
|
126
|
+
import { useMobxState, observer } from 'mobx-react-use-autorun';
|
|
127
|
+
import { useMobxEffect, toJS } from 'mobx-react-use-autorun'
|
|
128
|
+
|
|
129
|
+
export default observer(() => {
|
|
130
|
+
|
|
131
|
+
const state = useMobxState({
|
|
132
|
+
randomNumber: 1
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
useMobxEffect(() => {
|
|
136
|
+
console.log(toJS(state))
|
|
137
|
+
}, [state.randomNumber])
|
|
138
|
+
|
|
139
|
+
return <div onClick={() => state.randomNumber = Math.random()}>
|
|
140
|
+
{state.randomNumber}
|
|
141
|
+
</div>
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
### Define global mutable data with observable
|
|
145
|
+
|
|
146
|
+
// File: GlobalState.tsx
|
|
147
|
+
import { observable } from 'mobx-react-use-autorun';
|
|
148
|
+
|
|
149
|
+
export const globalState = observable({
|
|
150
|
+
age: 15,
|
|
151
|
+
name: 'tom'
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// File: UserComponent.tsx
|
|
155
|
+
import { observer } from "mobx-react-use-autorun";
|
|
156
|
+
import { globalState } from "./GlobalState";
|
|
157
|
+
|
|
158
|
+
export default observer(() => {
|
|
159
|
+
return <div
|
|
160
|
+
onClick={() => {
|
|
161
|
+
globalState.age++;
|
|
162
|
+
}}
|
|
163
|
+
>
|
|
164
|
+
{`${globalState.name}'s age is ${globalState.age}.`}
|
|
165
|
+
</div>;
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
### Get the real data of the proxy object with toJS
|
|
169
|
+
|
|
170
|
+
"toJS" will cause the component to re-render when data changes.<br/>
|
|
171
|
+
Please do not execute "toJS(state)" in component rendering code, it may cause repeated rendering.<br/>
|
|
172
|
+
Wrong Example:<br/>
|
|
173
|
+
|
|
174
|
+
import { toJS, observer, useMobxState } from 'mobx-react-use-autorun'
|
|
175
|
+
import { v1 } from 'uuid'
|
|
176
|
+
|
|
177
|
+
export default observer(() => {
|
|
178
|
+
|
|
179
|
+
const state = useMobxState({}, {
|
|
180
|
+
id: v1()
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
toJS(state)
|
|
184
|
+
|
|
185
|
+
return null;
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
Other than that, all usages are correct.<br/>
|
|
189
|
+
Correct Example:<br/>
|
|
190
|
+
|
|
191
|
+
import { toJS, useMobxEffect } from 'mobx-react-use-autorun';
|
|
192
|
+
import { observer, useMobxState } from 'mobx-react-use-autorun';
|
|
193
|
+
import { v1 } from 'uuid'
|
|
194
|
+
|
|
195
|
+
export default observer(() => {
|
|
196
|
+
|
|
197
|
+
const state = useMobxState({
|
|
198
|
+
name: v1()
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
useMobxEffect(() => {
|
|
202
|
+
console.log(toJS(state));
|
|
203
|
+
console.log(toJS(state.name));
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
console.log(toJS(state.name));
|
|
207
|
+
|
|
208
|
+
return <button
|
|
209
|
+
onClick={() => {
|
|
210
|
+
console.log(toJS(state));
|
|
211
|
+
console.log(toJS(state.name));
|
|
212
|
+
}}
|
|
213
|
+
>
|
|
214
|
+
{'Click Me'}
|
|
215
|
+
</button>;
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
# Notes - Work with non-observer components
|
|
219
|
+
|
|
220
|
+
Non-observer components cannot trigger re-rendering when the following data changes, please use "toJS" to do it.<br/>
|
|
221
|
+
* array<br/>
|
|
222
|
+
* object<br/>
|
|
223
|
+
* The all data used in the new render callback<br/>
|
|
224
|
+
|
|
225
|
+
<br/>
|
|
226
|
+
|
|
227
|
+
import { observer, toJS, useMobxState } from "mobx-react-use-autorun";
|
|
228
|
+
import { v1 } from "uuid";
|
|
229
|
+
import { Virtuoso } from 'react-virtuoso'
|
|
230
|
+
|
|
231
|
+
export default observer(() => {
|
|
232
|
+
|
|
233
|
+
const state = useMobxState({
|
|
234
|
+
userList: [{ id: 1, username: 'Tom' }]
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
toJS(state.userList)
|
|
238
|
+
|
|
239
|
+
return <Virtuoso
|
|
240
|
+
style={{ width: "500px", height: "200px" }}
|
|
241
|
+
data={state.userList}
|
|
242
|
+
itemContent={(index, item) =>
|
|
243
|
+
<div key={item.id} onClick={() => item.username = v1()}>
|
|
244
|
+
{item.username}
|
|
245
|
+
</div>
|
|
246
|
+
}
|
|
247
|
+
/>
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
# Notes - Work with typedjson
|
|
251
|
+
|
|
252
|
+
Typedjson is a strongly typed reflection library.<br/>
|
|
253
|
+
|
|
254
|
+
import { makeAutoObservable, toJS } from "mobx-react-use-autorun";
|
|
255
|
+
import { TypedJSON, jsonMember, jsonObject } from "typedjson";
|
|
256
|
+
|
|
257
|
+
@jsonObject
|
|
258
|
+
export class UserModel {
|
|
259
|
+
|
|
260
|
+
@jsonMember(String)
|
|
261
|
+
username!: string;
|
|
262
|
+
|
|
263
|
+
@jsonMember(Date)
|
|
264
|
+
createDate!: Date;
|
|
265
|
+
|
|
266
|
+
constructor() {
|
|
267
|
+
makeAutoObservable(this);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const user = new TypedJSON(UserModel).parse(`{"username":"tom","createDate":"2023-04-13T04:21:59.262Z"}`);
|
|
272
|
+
console.log(toJS(user));
|
|
273
|
+
|
|
274
|
+
const anotherUser = new TypedJSON(UserModel).parse({
|
|
275
|
+
username: "tom",
|
|
276
|
+
createDate: "2023-04-13T04:21:59.262Z"
|
|
277
|
+
});
|
|
278
|
+
console.log(toJS(anotherUser));
|
|
279
|
+
|
|
280
|
+
# Learn More
|
|
281
|
+
|
|
282
|
+
1. A JavaScript library for building user interfaces (https://react.dev)<br/>
|
|
283
|
+
2. Reactive Extensions Library for JavaScript (https://www.npmjs.com/package/rxjs)
|
|
284
|
+
3. Material UI is a library of React UI components that implements Google's Material Design (https://mui.com)
|
|
285
|
+
|
|
286
|
+
# Getting Started
|
|
287
|
+
|
|
288
|
+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). If you have any questions, please contact zdu.strong@gmail.com.<br/>
|
|
289
|
+
|
|
290
|
+
## Development environment setup
|
|
291
|
+
1. From https://code.visualstudio.com install Visual Studio Code.<br/>
|
|
292
|
+
2. From https://nodejs.org install nodejs v22.<br/>
|
|
293
|
+
|
|
294
|
+
## Available Scripts
|
|
295
|
+
|
|
296
|
+
In the project directory, you can run:<br/>
|
|
297
|
+
|
|
298
|
+
### `npm test`
|
|
299
|
+
|
|
300
|
+
Run all unit tests.<br/>
|
|
301
|
+
|
|
302
|
+
### `npm run build`
|
|
303
|
+
|
|
304
|
+
Builds the files for production to the `dist` folder.<br/>
|
|
305
|
+
|
|
306
|
+
### `npm run make`
|
|
307
|
+
|
|
308
|
+
Publish to npm repository<br/>
|
|
309
|
+
|
|
310
|
+
Pre-step, please run<br/>
|
|
311
|
+
|
|
312
|
+
npm login --registry https://registry.npmjs.org
|
|
313
|
+
|
|
314
|
+
# License
|
|
315
|
+
|
|
316
316
|
This project is licensed under the terms of the [MIT license](./LICENSE).
|