@@ -93,3 +93,116 @@ const styles = StyleSheet.create({
93
93
})
94
94
```
95
95
具体请参照官方文档[ 样式] ( https://reactnative.cn/docs/style )
96
+
97
+ ## Rematch
98
+
99
+ ### 第一步:建立models
100
+
101
+ ``` bash
102
+ mocker
103
+ ...
104
+ src
105
+ models
106
+ + home.js
107
+ pages
108
+ ...
109
+ package.json
110
+ ```
111
+
112
+ ``` js
113
+ export default {
114
+ name: ' home' ,
115
+ // initial state
116
+ state: {
117
+ num: 0
118
+ },
119
+ reducers: {
120
+ update : (state , payload ) => ({ ... state, ... payload }),
121
+ },
122
+ effects : (dispatch ) => ({
123
+ // 可以进行异步请求
124
+ }),
125
+ };
126
+ ```
127
+
128
+ ### 第二步:初始化store
129
+
130
+ ``` js
131
+ import { init } from ' @rematch/core' ;
132
+ import createLoadingPlugin from ' @rematch/loading' ;
133
+ import * as home from ' ./home' ;
134
+
135
+
136
+ const loadingPlugin = createLoadingPlugin ({});
137
+
138
+ export const store = init ({
139
+ models: {
140
+ home: home .default ,
141
+ },
142
+ plugins: [loadingPlugin],
143
+ });
144
+
145
+ ```
146
+
147
+ ### 第三步:页面中使用
148
+ ``` js
149
+ import React , { useState } from ' react' ;
150
+ import { View ,Text } from ' react-native'
151
+ import { Button } from ' @uiw/react-native' ;
152
+ import { connect } from ' react-redux' ;
153
+
154
+ const Demo = ({ num,update }) => {
155
+ const click = ()=> {
156
+ update ({
157
+ num: 10
158
+ })
159
+ }
160
+ return (
161
+ < View>
162
+ < Text > {num}< / Text >
163
+ < Button onPress= {click}> 点击< / Button>
164
+ < / View>
165
+ );
166
+ }
167
+
168
+ export default connect (
169
+ ({ home }) => ({
170
+ num: home .num
171
+ }),
172
+ ({ home }) => ({
173
+ update: home .update
174
+ }),
175
+ )(Demo);
176
+
177
+ ```
178
+
179
+ 我们也可以使用hooks调用
180
+ ``` js
181
+ import React , { useState } from ' react' ;
182
+ import { View ,Text } from ' react-native'
183
+ import { Button } from ' @uiw/react-native' ;
184
+ import { useSelector ,useDispatch } from ' react-redux' ;
185
+
186
+ const Demo = () => {
187
+ const dispatch = useDispatch ();
188
+ const { num } = useSelector (state => state .home )
189
+ const click = ()=> {
190
+ dispatch ({
191
+ type: ' home/update' ,
192
+ payload: {
193
+ num: 10
194
+ }
195
+ })
196
+ }
197
+ return (
198
+ < View>
199
+ < Text > {num}< / Text >
200
+ < Button onPress= {click}> 点击< / Button>
201
+ < / View>
202
+ );
203
+ }
204
+
205
+ export default Demo
206
+
207
+ ```
208
+ 具体请参照官方文档[ Rematch] ( https://rematchjs.org/docs/ )
0 commit comments