4
4
5
5
针对 [ react-native] ( https://reactnative.dev/ ) 应用的 [ umi] ( https://umijs.org/ ) 插件集。
6
6
7
- ** 零配置** ,添加[ dva ] ( https://dvajs.com/ ) ,[ @ant-design/react-native ] ( https://rn.mobile.ant.design/index-cn ) ... 依赖后开箱即用。
8
-
9
- 默认使用 [ react-router ] ( https://reacttraining.com/react-router/ ) ,可选 [ react-navigation ] ( https://reactnavigation.org/ ) 。
7
+ - ** 零配置** ,添加[ DvaJS ] ( https://dvajs.com/ ) ,[ @ant-design/react-native ] ( https://rn.mobile.ant.design/index-cn ) ... 依赖后开箱即用,只需专注于业务代码;
8
+ - 路由方案默认使用 umi 内置的 [ react-router ] ( https://reacttraining.com/react-router/ ) ,可平滑替换为 [ react-navigation ] ( https://reactnavigation.org/ ) ;
9
+ - 支持切分多 bundle,运行时按需加载 bundle,降低内存开销,提升首屏加载速度;
10
10
11
11
[ 发布日志] ( /CHANGELOG.md )
12
12
13
13
## 目录
14
14
15
15
- [ 必备] ( #%E5%BF%85%E5%A4%87 )
16
16
- [ 安装] ( #%E5%AE%89%E8%A3%85 )
17
- - [ 集成 dva ] ( #%E9%9B%86%E6%88%90-dva )
17
+ - [ 集成 DvaJS ] ( #%E9%9B%86%E6%88%90-dvajs )
18
18
- [ 集成 @ant-design/react-native ] ( #%E9%9B%86%E6%88%90-ant-designreact-native )
19
19
- [ 使用] ( #%E4%BD%BF%E7%94%A8 )
20
20
- [ 配置] ( #%E9%85%8D%E7%BD%AE )
21
21
- [ 目前支持的 umi 配置项] ( #%E7%9B%AE%E5%89%8D%E6%94%AF%E6%8C%81%E7%9A%84-umi-%E9%85%8D%E7%BD%AE%E9%A1%B9 )
22
22
- [ umi-preset-react-native 扩展配置] ( #umi-preset-react-native-%E6%89%A9%E5%B1%95%E9%85%8D%E7%BD%AE )
23
- - [ 路由] ( #%E8%B7%AF%E7%94%B1 )
24
- - [ react-router] ( #react-router )
25
- - [ ` Link ` 组件在 RN 和 DOM 中存在差异] ( #link%E7%BB%84%E4%BB%B6%E5%9C%A8-rn-%E5%92%8C-dom-%E4%B8%AD%E5%AD%98%E5%9C%A8%E5%B7%AE%E5%BC%82 )
26
- - [ 没有` NavLink ` 组件] ( #%E6%B2%A1%E6%9C%89navlink%E7%BB%84%E4%BB%B6 )
27
- - [ 新增` BackButton ` 和` AndroidBackButton ` 组件] ( #%E6%96%B0%E5%A2%9Ebackbutton%E5%92%8Candroidbackbutton%E7%BB%84%E4%BB%B6 )
28
- - [ react-navigation] ( #react-navigation )
29
23
- [ 开发] ( #%E5%BC%80%E5%8F%91 )
30
24
- [ 打包] ( #%E6%89%93%E5%8C%85 )
25
+ - [ 路由] ( #%E8%B7%AF%E7%94%B1 )
26
+ - [ 使用 umi 内置的 react-router] ( #%E4%BD%BF%E7%94%A8-umi-%E5%86%85%E7%BD%AE%E7%9A%84-react-router )
27
+ - [ ` Link ` 组件在 RN 和 DOM 中存在差异] ( #link%E7%BB%84%E4%BB%B6%E5%9C%A8-rn-%E5%92%8C-dom-%E4%B8%AD%E5%AD%98%E5%9C%A8%E5%B7%AE%E5%BC%82 )
28
+ - [ 没有` NavLink ` 组件] ( #%E6%B2%A1%E6%9C%89navlink%E7%BB%84%E4%BB%B6 )
29
+ - [ 新增` BackButton ` 和` AndroidBackButton ` 组件] ( #%E6%96%B0%E5%A2%9Ebackbutton%E5%92%8Candroidbackbutton%E7%BB%84%E4%BB%B6 )
30
+ - [ 替换为 react-navigation] ( #%E6%9B%BF%E6%8D%A2%E4%B8%BA-react-navigation )
31
31
- [ 示例] ( #%E7%A4%BA%E4%BE%8B )
32
32
- [ 深入] ( #%E6%B7%B1%E5%85%A5 )
33
33
- [ 切分多 bundle] ( #%E5%88%87%E5%88%86%E5%A4%9A-bundle )
34
34
- [ FAQ] ( #faq )
35
- - [ ` hmrClient.send is not a function ` ] ( #hmrclientsend-is-not-a-function )
36
- - [ ` Live Reloading ` , ` Fast Refresh ` , ` Hot Replacement ` 无法使用] ( #live-reloading-fast-refresh-hot-replacement%E6%97%A0%E6%B3%95%E4%BD%BF%E7%94%A8 )
35
+ - [ hmrClient.send is not a function] ( #hmrclientsend-is-not-a-function )
36
+ - [ Live Reloading, Fast Refresh, Hot Replacement... 无法使用] ( #live-reloading-fast-refresh-hot-replacement-%E6%97%A0%E6%B3%95%E4%BD%BF%E7%94%A8 )
37
+ - [ @ant-design/react-native 组件没有工作(比如:弹窗打不开等等)] ( #ant-designreact-native-%E7%BB%84%E4%BB%B6%E6%B2%A1%E6%9C%89%E5%B7%A5%E4%BD%9C%E6%AF%94%E5%A6%82%E5%BC%B9%E7%AA%97%E6%89%93%E4%B8%8D%E5%BC%80%E7%AD%89%E7%AD%89 )
38
+ - [ 使用@ant-design/react-native 组件时,报错:Unrecognized font family 'antoutline'] ( #%E4%BD%BF%E7%94%A8ant-designreact-native-%E7%BB%84%E4%BB%B6%E6%97%B6%E6%8A%A5%E9%94%99unrecognized-font-family-antoutline )
37
39
38
40
## 必备
39
41
@@ -70,22 +72,39 @@ npx react-native init UMIRNExample
70
72
yarn add umi umi-preset-react-native --dev
71
73
```
72
74
73
- ### 集成 dva
75
+ ### 集成 DvaJS
74
76
75
77
在 RN 工程根目录下使用 yarn 添加` @umijs/plugin-dva ` 依赖:
76
78
77
79
``` npm
78
80
yarn add @umijs/plugin-dva --dev
79
81
```
80
82
83
+ _ 待 yarn 安装完成后开箱即用。_
84
+
81
85
### 集成 @ant-design/react-native
82
86
83
- 在 RN 工程根目录下使用 yarn 添加[ umi-plugin-antd-react-native] ( /packages/umi-plugin-antd-react-native/README.md ) 依赖:
87
+ 在 RN 工程根目录下使用 yarn 添加` @ant-design/react-native ` 和` @ant-design/icons-react-native ` 依赖:
88
+
89
+ ``` npm
90
+ yarn add @ant-design/react-native @ant-design/icons-react-native
91
+ ```
92
+
93
+ ** RN` 0.60.0 ` 及以上版本** 会[ 自动链接] ( https://github.com/react-native-community/cli/blob/master/docs/autolinking.md ) ` @ant-design/icons-react-native ` ,待 yarn 安装完成后,如果是 iOS 需要:
94
+
95
+ ``` shell
96
+ cd ios && pod install
97
+ ```
98
+
99
+ ** RN` 0.50.x ` 版本** ,待 yarn 安装完成,还需要使用` react-native link ` ** 手动链接** ` @ant-design/icons-react-native ` :
84
100
85
101
``` npm
86
- yarn add umi-plugin-antd-react-native --dev
102
+ yarn react-native link @ant-design/icons-react-native
103
+ # ./node_modules/.bin/react-native link @ant-design/icons-react-native
87
104
```
88
105
106
+ ** umi-preset-react-native** 已经为` @ant-design/react-native ` 添加了[ ** 按需加载** ] ( https://rn.mobile.ant.design/docs/react/introduce-cn#%E6%8C%89%E9%9C%80%E5%8A%A0%E8%BD%BD ) ([ babel-plugin-import] ( https://github.com/ant-design/babel-plugin-import ) )的配置。
107
+
89
108
## 使用
90
109
91
110
### 配置
@@ -123,7 +142,7 @@ _与 DOM 无关的[umi](https://umijs.org/)插件都是可以使用的,或者
123
142
- [x] [ routes] ( https://umijs.org/config#routes )
124
143
- [x] [ singular] ( https://umijs.org/config#singular )
125
144
126
- _ 上文未列出的[ umi 配置] ( https://umijs.org/config ) 对 ** umi-preset-react-native** 不生效。RN 开发不同于 Web 开发,基本上无须在编译工具上做过多配置。 _
145
+ _ 上文未列出的[ umi 配置] ( https://umijs.org/config ) 对 ** umi-preset-react-native** 不生效。_
127
146
128
147
[ haul] ( https://github.com/callstack/haul ) 使用的 devServer 是[ hapi] ( https://hapi.dev/ ) ,目前还不支持扩展额外的 hapi 插件(中间件),暂时无法支持[ mock] ( https://umijs.org/config#mock ) 和[ proxy] ( https://umijs.org/config#proxy ) 功能。
129
148
@@ -146,22 +165,68 @@ export default {
146
165
};
147
166
```
148
167
149
- - ` reactNative ` :选填,默认值:上面代码示例中的值
168
+ - ` reactNative ` :选填,默认值:上面代码示例中的值;
150
169
- ` haul ` :选填,默认值:上面代码示例中的值,即[ Project Configuration] ( https://github.com/callstack/haul/blob/master/docs/Configuration.md#project-configuration-reference ) 。
151
170
152
- ** 注意** :不需要使用 haul 提供的工具:` makeConfig ` 和` withPolyfills ` 。直接填入[ Project Configuration] ( https://github.com/callstack/haul/blob/master/docs/Configuration.md#project-configuration-reference ) 的字段即可。
171
+ ### 开发
172
+
173
+ 修改` package.json ` 文件,使用` umi ` 取代` react-native ` :
174
+
175
+ ``` diff
176
+ {
177
+ "scripts": {
178
+ "android": "react-native run-android",
179
+ "ios": "react-native run-ios",
180
+ - "start": "react-native start",
181
+ + "start": "umi dev-rn",
182
+ },
183
+ }
184
+ ```
185
+
186
+ 执行` yarn start ` 之后,再使用 ` yarn android ` 或者 ` yarn ios ` 。
187
+
188
+ ### 打包
189
+
190
+ 构建离线包(offline bundle):
191
+
192
+ ``` shell
193
+ umi build-rn --platform < ios| android> --bundle-output < relative/to/output/path/filename> --assets-dest < relative/to/output/path>
194
+ ```
153
195
154
- 在做多 bundle 切分时,要保证主 bundle 中必须包含 ` ./umi.ts ` 。
196
+ _ ` relative/to/output/path ` : 表示相对于 [ outputPath ] ( https://umijs.org/config#outputpath ) 的路径。 _
155
197
156
- ### 路由
198
+ package.json:
157
199
158
- #### react-router
200
+ ``` diff
201
+ {
202
+ "scripts": {
203
+ "android": "react-native run-android",
204
+ "ios": "react-native run-ios",
205
+ "start": "umi dev-rn",
206
+ + "build:ios": "NODE_ENV=production umi build-rn --platform ios --bundle-output ios/main.jsbundle --assets-dest ios/assets",
207
+ + "build:android": "NODE_ENV=production umi build-rn --platform android --bundle-output android/index.android.bundle --assets-dest android/assets"
208
+ },
209
+ }
210
+ ```
211
+
212
+ - 执行` yarn build:ios ` :
213
+ 1 . 构建生成` main.jsbundle ` 文件到` dist/ios/ ` 目录;
214
+ 2 . 拷贝静态资源到` dist/ios/assets/ ` 目录。
215
+ - 执行` yarn build:android ` :
216
+ 1 . 构建生成` index.android.bundle ` 文件到` dist/anrdoid/ ` 目录;
217
+ 2 . 拷贝静态资源到` dist/android/assets/ ` 目录。
218
+
219
+ _ ` dist ` 是[ outputPath] ( https://umijs.org/config#outputpath ) 配置项的缺省(默认)值,可在` .umirc.js ` 中配置其他目录。_
220
+
221
+ ## 路由
222
+
223
+ ### 使用 umi 内置的 react-router
159
224
160
225
[ umi] ( https://umijs.org/ ) 内置了` react-router-dom ` ,** umi-preset-react-native** 在运行时会将其替换为:` react-router-native ` 。
161
226
162
227
二者都基于 [ react-router] ( https://reacttraining.com/react-router/ ) ,但存在一些差异。
163
228
164
- ##### ` Link ` 组件在 RN 和 DOM 中存在差异
229
+ #### ` Link ` 组件在 RN 和 DOM 中存在差异
165
230
166
231
以下是` react-router-native ` ` Link ` 组件的属性:
167
232
@@ -196,7 +261,7 @@ function Index() {
196
261
}
197
262
```
198
263
199
- ##### 没有` NavLink ` 组件
264
+ #### 没有` NavLink ` 组件
200
265
201
266
` react-router-native ` 没有` NavLink ` 组件,当你尝试引入时会得到` undefined ` :
202
267
@@ -206,7 +271,7 @@ import { NavLink } from 'umi';
206
271
typeof NavLink === ' undefined' ; // true;
207
272
```
208
273
209
- ##### 新增` BackButton ` 和` AndroidBackButton ` 组件
274
+ #### 新增` BackButton ` 和` AndroidBackButton ` 组件
210
275
211
276
对于 RN 应用,需要在[ 全局 layout] ( https://umijs.org/docs/convention-routing#%E5%85%A8%E5%B1%80-layout ) 中使用` BackButton ` 作为根容器:
212
277
@@ -230,61 +295,11 @@ export default Layout;
230
295
231
296
这样做,当用户使用** 系统返回键** 时会返回应用的上一个路由,而不是退出应用。
232
297
233
- #### react-navigation
298
+ ### 替换为 react-navigation
234
299
235
300
[ react-navigation] ( https://reactnavigation.org/ ) 可作为 umi 默认[ react-router] ( https://reacttraining.com/react-router/ ) 的** 替代方案** 。
236
301
237
- 了解详情,请移步至:<a href =" https://github.com/xuyuanxiang/umi-react-native/tree/master/packages/umi-plugin-react-navigation#readme " target =" _blank " >umi-plugin-react-navigation 文档</a >。
238
-
239
- ### 开发
240
-
241
- 修改` package.json ` 文件,使用` umi ` 取代` react-native ` :
242
-
243
- ``` diff
244
- {
245
- "scripts": {
246
- "android": "react-native run-android",
247
- "ios": "react-native run-ios",
248
- - "start": "react-native start",
249
- + "start": "umi dev-rn",
250
- },
251
- }
252
- ```
253
-
254
- 执行` yarn start ` 之后,再使用 ` yarn android ` 或者 ` yarn ios ` 。
255
-
256
- ### 打包
257
-
258
- 构建离线包(offline bundle):
259
-
260
- ``` shell
261
- umi build-rn --platform < ios| android> --bundle-output < relative/to/output/path/filename> --assets-dest < relative/to/output/path>
262
- ```
263
-
264
- _ ` relative/to/output/path ` : 表示相对于[ outputPath] ( https://umijs.org/config#outputpath ) 的路径。_
265
-
266
- package.json:
267
-
268
- ``` diff
269
- {
270
- "scripts": {
271
- "android": "react-native run-android",
272
- "ios": "react-native run-ios",
273
- "start": "umi dev-rn",
274
- + "build:ios": "NODE_ENV=production umi build-rn --platform ios --bundle-output ios/main.jsbundle --assets-dest ios/assets",
275
- + "build:android": "NODE_ENV=production umi build-rn --platform android --bundle-output android/index.android.bundle --assets-dest android/assets"
276
- },
277
- }
278
- ```
279
-
280
- - 执行` yarn build:ios ` :
281
- 1 . 构建生成` main.jsbundle ` 文件到` dist/ios/ ` 目录;
282
- 2 . 拷贝静态资源到` dist/ios/assets/ ` 目录。
283
- - 执行` yarn build:android ` :
284
- 1 . 构建生成` index.android.bundle ` 文件到` dist/anrdoid/ ` 目录;
285
- 2 . 拷贝静态资源到` dist/android/assets/ ` 目录。
286
-
287
- _ ` dist ` 是[ outputPath] ( https://umijs.org/config#outputpath ) 配置项的缺省(默认)值,可在` .umirc.js ` 中配置其他目录。_
302
+ 了解详情,请移步至:<a href =" https://github.com/xuyuanxiang/umi-react-native/tree/master/packages/umi-plugin-react-navigation#readme " target =" _blank " >umi-plugin-react-navigation</a >。
288
303
289
304
## 示例
290
305
@@ -338,9 +353,14 @@ export default {
338
353
};
339
354
```
340
355
356
+ ** 注意** :
357
+
358
+ - ** 不要** 使用 haul 提供的工具:` makeConfig ` 和` withPolyfills ` ;
359
+ - ** 要** 保证主 bundle 中必须包含:` ./umi.ts ` 。
360
+
341
361
## FAQ
342
362
343
- ### ` hmrClient.send is not a function `
363
+ ### hmrClient.send is not a function
344
364
345
365
当出现以下错误时需要升级` metro ` 至` ^0.56.0 ` :[ react-native#issue-26958] ( https://github.com/facebook/react-native/issues/26958 ) 。
346
366
@@ -356,6 +376,26 @@ _在 RN 工程`node_modules`目录中找到`metro`并查看版本:_
356
376
cat ./node_modules/metro/package.json | grep version
357
377
```
358
378
359
- ### ` Live Reloading ` , ` Fast Refresh ` , ` Hot Replacement ` 无法使用
379
+ ### Live Reloading, Fast Refresh, Hot Replacement... 无法使用
360
380
361
381
[ haul] ( https://github.com/callstack/haul ) 不支持:[ haul#issue-682] ( https://github.com/callstack/haul/issues/682 ) 。
382
+
383
+ ### @ant-design/react-native 组件没有工作(比如:弹窗打不开等等)
384
+
385
+ 按照[ @ant-design/react-native ] ( https://rn.mobile.ant.design/docs/react/introduce-cn ) 文档示例使用组件时,未能达到预期效果,可能是因为没有使用` Provider ` 作为根节点:
386
+
387
+ ``` jsx
388
+ // <projectRoot>/layouts/index.js
389
+ import React from ' react' ;
390
+ import { Provider } from ' @ant-design/react-native' ;
391
+
392
+ function Layout ({ children }) {
393
+ return < Provider> {children}< / Provider> ;
394
+ }
395
+
396
+ export default Layout ;
397
+ ```
398
+
399
+ ### 使用@ant-design/react-native 组件时,报错:Unrecognized font family 'antoutline'
400
+
401
+ [ ant-design/ant-design-mobile-rn#issue-194] ( https://github.com/ant-design/ant-design-mobile-rn/issues/194 ) 中有解决方案。
0 commit comments