mobx-react-use-autorun 4.0.23 → 4.0.26
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 +38 -43
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,10 +8,9 @@ Provide concise usage for mobx in react<br/>
|
|
|
8
8
|
|
|
9
9
|
### Define state with useMobxState
|
|
10
10
|
|
|
11
|
-
import {
|
|
12
|
-
import { useRef } from 'react';
|
|
11
|
+
import { observer, useMobxState } from 'mobx-react-use-autorun';
|
|
13
12
|
|
|
14
|
-
export default observer((
|
|
13
|
+
export default observer(() => {
|
|
15
14
|
|
|
16
15
|
const state = useMobxState({
|
|
17
16
|
age: 16
|
|
@@ -20,38 +19,10 @@ Provide concise usage for mobx in react<br/>
|
|
|
20
19
|
return <div
|
|
21
20
|
onClick={() => state.age++}
|
|
22
21
|
>
|
|
23
|
-
{
|
|
22
|
+
{`John's age is ${state.age}`}
|
|
24
23
|
</div>
|
|
25
24
|
})
|
|
26
25
|
|
|
27
|
-
useMobxState provides two usages.<br/>
|
|
28
|
-
|
|
29
|
-
The first way is easy to use, you can define state, props and third-party hooks.<br/>
|
|
30
|
-
|
|
31
|
-
useMobxState({
|
|
32
|
-
name: 'tom',
|
|
33
|
-
age: 16,
|
|
34
|
-
myInfo(){
|
|
35
|
-
return `${state.name}'s age is ${state.age}`
|
|
36
|
-
},
|
|
37
|
-
}, {
|
|
38
|
-
...props,
|
|
39
|
-
intl: useIntl(),
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
The second way provide a method to generate state, the state is executed only once, and the performance is better.<br/>
|
|
43
|
-
|
|
44
|
-
useMobxState(() => ({
|
|
45
|
-
name: 'tom',
|
|
46
|
-
age: 13,
|
|
47
|
-
myInfo(){
|
|
48
|
-
return `${state.name}'s age is ${state.age}`
|
|
49
|
-
},
|
|
50
|
-
}), {
|
|
51
|
-
...props,
|
|
52
|
-
intl: useIntl(),
|
|
53
|
-
})
|
|
54
|
-
|
|
55
26
|
More example - Form validation:<br/>
|
|
56
27
|
|
|
57
28
|
import { Button, TextField } from '@mui/material';
|
|
@@ -101,24 +72,28 @@ More example - Form validation:<br/>
|
|
|
101
72
|
</div>)
|
|
102
73
|
})
|
|
103
74
|
|
|
104
|
-
|
|
75
|
+
More example - Use props and other hooks:<br/>
|
|
105
76
|
|
|
106
|
-
import {
|
|
107
|
-
import {
|
|
77
|
+
import { observer, useMobxState } from 'mobx-react-use-autorun';
|
|
78
|
+
import { useRef } from 'react';
|
|
108
79
|
|
|
109
|
-
|
|
80
|
+
|
|
81
|
+
export default observer((props: { user: { name: string, age: number } }) => {
|
|
110
82
|
|
|
111
83
|
const state = useMobxState({
|
|
112
|
-
|
|
84
|
+
}, {
|
|
85
|
+
containerRef: useRef<HTMLDivElement>(null),
|
|
113
86
|
});
|
|
114
87
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
88
|
+
return <div
|
|
89
|
+
ref={state.containerRef}
|
|
90
|
+
onClick={() => {
|
|
91
|
+
props.user.age++;
|
|
92
|
+
}}
|
|
93
|
+
>
|
|
94
|
+
{`${props.user.name}'s age is ${props.user.age}.`}
|
|
121
95
|
</div>
|
|
96
|
+
|
|
122
97
|
})
|
|
123
98
|
|
|
124
99
|
### Lifecycle hook with useMount
|
|
@@ -145,6 +120,26 @@ Strict Mode: In the future, React will provide a feature that lets components pr
|
|
|
145
120
|
return null;
|
|
146
121
|
})
|
|
147
122
|
|
|
123
|
+
### Subscription property changes with useMobxEffect
|
|
124
|
+
|
|
125
|
+
import { useMobxState, observer } from 'mobx-react-use-autorun';
|
|
126
|
+
import { useMobxEffect, toJS } from 'mobx-react-use-autorun'
|
|
127
|
+
|
|
128
|
+
export default observer(() => {
|
|
129
|
+
|
|
130
|
+
const state = useMobxState({
|
|
131
|
+
randomNumber: 1
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
useMobxEffect(() => {
|
|
135
|
+
console.log(toJS(state))
|
|
136
|
+
}, [state.randomNumber])
|
|
137
|
+
|
|
138
|
+
return <div onClick={() => state.randomNumber = Math.random()}>
|
|
139
|
+
{state.randomNumber}
|
|
140
|
+
</div>
|
|
141
|
+
})
|
|
142
|
+
|
|
148
143
|
### Define global mutable data with observable
|
|
149
144
|
|
|
150
145
|
// File: GlobalState.tsx
|
package/package.json
CHANGED