Skip to content

Commit 9411fd9

Browse files
author
徐远翔
committed
feat: 新增umi-preset-react-navigation包,支持使用react-navigation替换react-router。
1 parent eaba579 commit 9411fd9

File tree

15 files changed

+214
-169
lines changed

15 files changed

+214
-169
lines changed

packages/umi-plugin-antd-react-native/README.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
针对[@ant-design/react-native](https://rn.mobile.ant.design/index-cn)[umi](https://umijs.org/)插件。
44

5+
_了解如何使用[umi](https://umijs.org/)开发 RN 应用,请移步至:_<a href="https://github.com/xuyuanxiang/umi-react-native#readme" target="_blank">umi-react-native</a>
6+
57
## 安装
68

79
在 RN 工程目录下,使用 yarn 安装**umi-plugin-antd-react-native**
@@ -10,7 +12,7 @@
1012
yarn add umi-plugin-antd-react-native --dev
1113
```
1214

13-
在 RN 工程目录下,使用 yarn 安装[@ant-design/react-native](https://github.com/ant-design/ant-design-mobile-rn/blob/master/README.zh-CN.md#%E5%AE%89%E8%A3%85--%E4%BD%BF%E7%94%A8):
15+
在 RN 工程目录下,使用 yarn 安装`@ant-design/react-native@3.x`:
1416

1517
```npm
1618
yarn add @ant-design/react-native
@@ -19,17 +21,20 @@ yarn add @ant-design/react-native
1921
**链接 @ant-design/react-native 字体图标资源文件:**
2022

2123
```npm
22-
yarn react-native link # 等价于: ./node_modules/.bin/react-native link
24+
yarn react-native link
25+
# 等价于: ./node_modules/.bin/react-native link
2326
```
2427

28+
如需使用`@ant-design/react-native@4.x`请按照:[安装 & 使用](https://github.com/ant-design/ant-design-mobile-rn/blob/master/README.zh-CN.md#%E5%AE%89%E8%A3%85--%E4%BD%BF%E7%94%A8)操作。
29+
30+
## 使用
31+
2532
**umi-plugin-antd-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))的配置。
2633

2734
如果使用 @ant-design/react-native 组件时,出现错误信息:`Unrecognized font family 'antoutline'`
2835

2936
[ant-design/ant-design-mobile-rn#issue-194](https://github.com/ant-design/ant-design-mobile-rn/issues/194)中有解决方案,需要重新链接字体图标。
3037

31-
## 使用
32-
3338
### 自定义主题/皮肤
3439

3540
目前已经支持:使用 umi [theme 配置](https://umijs.org/config#theme)覆盖 @ant-design/react-native[主题](https://github.com/ant-design/ant-design-mobile-rn/blob/master/components/style/themes/default.tsx)

packages/umi-preset-react-navigation/README.md

+36-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# umi-preset-react-navigation
22

3-
支持在 RN 应用中 使用 [react-navigation](https://reactnavigation.org/) 替换 [umi](https://umijs.org/) 内置的 [react-router](https://reacttraining.com/react-router/)
3+
针对 [react-navigation](https://reactnavigation.org/)[umi](https://umijs.org/) 插件集。
4+
5+
支持在 RN 应用中替换 [umi](https://umijs.org/) 默认的 [react-router](https://reacttraining.com/react-router/)
6+
7+
内置[react-navigation](https://reactnavigation.org/) `5.x` 版本,需要 [react-native](https://reactnative.dev/) **0.60.0 及以上版本(>=0.60.x)**
48

59
_了解如何使用[umi](https://umijs.org/)开发 RN 应用,请移步至:_<a href="https://github.com/xuyuanxiang/umi-react-native#readme" target="_blank">umi-react-native</a>
610

@@ -20,13 +24,9 @@ _了解如何使用[umi](https://umijs.org/)开发 RN 应用,请移步至:_<
2024
- react-native-safe-area-context
2125
- @react-native-community/masked-view
2226

23-
**强烈推荐**使用 RN 0.60.0 及以上版本,否则安装会及其繁琐。
24-
25-
### RN`0.60.x`版本
26-
27-
**umi-preset-react-navigation**内置了[react-navigation](https://reactnavigation.org/)所有依赖最新版本。
27+
**umi-preset-react-navigation**内置了上面所有依赖。
2828

29-
RN 工程根目录,使用 yarn 安装**umi-preset-react-navigation**即可:
29+
只需在 RN 工程根目录,使用 yarn 安装**umi-preset-react-navigation**即可:
3030

3131
```npm
3232
yarn add umi-preset-react-navigation --dev
@@ -40,39 +40,28 @@ cd ios && pod install
4040

4141
**注意:**
4242

43-
虽然有[自动链接](https://github.com/react-native-community/cli/blob/master/docs/autolinking.md)功能,如果还是出现了原生代码报错,在 RN 工程根目录下手动重新链接一下
43+
虽然有[自动链接](https://github.com/react-native-community/cli/blob/master/docs/autolinking.md)功能,如果还是发生了意外,在 RN 工程根目录下手动重新链接一下可以解决大部分的出现了原生代码报错
4444

4545
```npm
4646
yarn react-native unlink && yarn react-native link
4747
# ./node_modules/.bin/react-native unlink && ./node_modules/.bin/react-native link
4848
```
4949

50-
每次添加原生依赖,都需要执行:`yarn ios``yarn android`重新编译和启动 iOS 和 Android 工程。
51-
52-
**RN`0.60.x`版本安装至此结束。**
53-
54-
### RN`0.50.x`版本
55-
56-
按照[react-navigation](https://reactnavigation.org/)[安装文档](https://reactnavigation.org/docs/getting-started/#installation)操作,将所有[react-navigation](https://reactnavigation.org/)所需依赖安装到 RN 工程本地。
57-
58-
**一定要逐一查看这些依赖的 README 文档**,因为:
59-
60-
1. 需要根据 RN 版本安装对应依赖的版本,以[react-native-gesture-handler](https://github.com/software-mansion/react-native-gesture-handler#react-native-support)为例,RN 0.57.2 及以上版本,需要安装`react-native-gesture-handler@1.1.x`**不能使用最新版**
61-
2. 有些[react-navigation](https://reactnavigation.org/)的依赖使用 yarn/npm 安装完成,还需要**手动链接**原生 iOS 和 Android 代码,可以按照这些依赖的 README 文档操作。
50+
每次添加原生依赖后,都需要执行:`yarn ios``yarn android`重新编译和启动 iOS 和 Android 工程。
6251

6352
## 扩展配置
6453

6554
以下是安装**umi-preset-react-navigation**后,扩展的 umi [配置](https://umijs.org/zh-CN/docs/config)
6655

6756
### reactNavigation
6857

69-
全都是选填参数,下面示例中填入的是默认值,等价于不填:
58+
`theme`字段选填,下面示例中填入的是默认值,等价于不填:
7059

7160
```javascript
7261
// .umirc.js
7362
export default {
7463
reactNavigation: {
75-
// 使用 ant-design 默认配色作为 Navigation 的默认主题
64+
// 使用 ant-design 默认配色作为导航条的默认主题
7665
theme: {
7766
dark: false,
7867
colors: {
@@ -83,18 +72,10 @@ export default {
8372
border: '#dddddd',
8473
},
8574
},
86-
type: 'stack', // Navigator 类型
87-
enableSafeAreasSupport: true, // 默认启用对 iOS Safe Areas 的适配/支持
8875
},
8976
};
9077
```
9178

92-
| 变量名 | 类型 | 说明 | 默认值 |
93-
| :--------------------- | :------ | :----------------------------------------------------- | ---------------------: |
94-
| type | enum | 导航类型,可选值:`'stack'`, `drawer`, `bottom-tabs`| 'stack' |
95-
| enableSafeAreasSupport | boolean | 是否适配 iOS Safe Areas。 | true |
96-
| theme | object | 初始主题,将来考虑支持运行时切换主题/皮肤。 | _上面代码示例中所填值_ |
97-
9879
## 扩展运行时配置
9980

10081
查看 umi 文档,了解什么是:[运行时配置](https://umijs.org/zh-CN/docs/runtime-config)
@@ -234,13 +215,34 @@ export default HomePage;
234215

235216
**如果页面的`title`属性未设置,则使用`.umirc.js`中的全局[title](https://umijs.org/config#title)**
236217

237-
## 应用内部页面跳转
218+
## 页面间跳转
238219

239-
### 使用命令式
220+
查看 umi 文档:[页面间跳转](https://umijs.org/zh-CN/docs/navigate-between-pages),姿势保持不变。
240221

241-
### 使用申明式
222+
使用**声明式**`Link`组件时需要注意,在 RN 中 与 DOM 存在较大差异:
223+
224+
```jsx
225+
import React from 'react';
226+
import { Link } from 'umi';
227+
import { List } from '@ant-design/react-native';
228+
229+
const Item = List.Item;
230+
231+
function Index() {
232+
return (
233+
<List>
234+
<Link to="/home" component={Item} arrow="horizontal">
235+
主页
236+
</Link>
237+
<Link to="/login" component={Item} arrow="horizontal">
238+
登录页
239+
</Link>
240+
</List>
241+
);
242+
}
243+
```
242244

243-
### 传递和接收参数
245+
使用**命令式**跳转页面时,只能使用`history`的 API,**umi-preset-react-navigation**目前还不支持使用[react-navigation](https://reactnavigation.org/)提供的`navigation`来跳转,只能做导航条设置之类的操作。
244246

245247
## DeepLink
246248

packages/umi-preset-react-navigation/package.json

-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343
},
4444
"dependencies": {
4545
"@react-native-community/masked-view": "0.1.10",
46-
"@react-navigation/bottom-tabs": "5.4.2",
47-
"@react-navigation/drawer": "5.7.2",
4846
"@react-navigation/native": "5.3.0",
4947
"@react-navigation/stack": "5.3.3",
5048
"react-native-gesture-handler": "1.6.1",

packages/umi-preset-react-navigation/src/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export default function () {
22
return {
33
plugins: [
4-
require.resolve('./plugins/generateFiles/react-navigation/createNavigator'),
54
require.resolve('./plugins/generateFiles/react-navigation/exports'),
65
require.resolve('./plugins/generateFiles/react-navigation/runtime'),
76
require.resolve('./plugins/features/reactNavigation'),

packages/umi-preset-react-navigation/src/plugins/features/reactNavigation.ts

-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@ export default (api: IApi) => {
1616
border: '#dddddd',
1717
},
1818
},
19-
type: 'stack', // Navigator 类型
20-
enableSafeAreasSupport: true, // 默认启用对 iOS Safe Areas 的适配/支持
2119
},
2220
schema(joi) {
2321
return joi
2422
.object({
25-
type: joi.string().allow('stack', 'drawer', 'bottom-tabs').optional(),
2623
theme: joi
2724
.object({
2825
dark: joi.boolean().required(),
@@ -35,7 +32,6 @@ export default (api: IApi) => {
3532
}),
3633
})
3734
.optional(),
38-
enableSafeAreasSupport: joi.boolean().optional(),
3935
})
4036
.optional();
4137
},

packages/umi-preset-react-navigation/src/plugins/generateFiles/react-navigation/createNavigator.ts

-29
This file was deleted.

packages/umi-preset-react-navigation/src/plugins/generateFiles/react-navigation/exports.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IApi } from 'umi';
22

3-
const exportsTpl = `export {
3+
const CONTENT = `export {
44
useNavigation,
55
useRoute as useScreen,
66
useFocusEffect,
@@ -14,23 +14,20 @@ const exportsTpl = `export {
1414
CommonActions,
1515
StackActions,
1616
} from '@react-navigation/native';
17-
{{#enableSafeAreasSupport}}
1817
export {
1918
SafeAreaProvider,
2019
SafeAreaView,
2120
useSafeAreaInsets,
2221
useSafeAreaFrame,
2322
} from 'react-native-safe-area-context';
24-
{{/enableSafeAreasSupport}}
23+
2524
`;
2625

2726
export default (api: IApi) => {
2827
api.onGenerateFiles(() => {
2928
api.writeTmpFile({
3029
path: 'react-navigation/exports.ts',
31-
content: api.utils.Mustache.render(exportsTpl, {
32-
enableSafeAreasSupport: Boolean(api.config?.reactNavigation?.enableSafeAreasSupport),
33-
}),
30+
content: CONTENT,
3431
});
3532
});
3633

packages/umi-preset-react-navigation/src/plugins/generateFiles/react-navigation/runtime.ts

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export default (api: IApi) => {
3636
api.writeTmpFile({
3737
path: 'react-navigation/runtime.tsx',
3838
content: Mustache.render(runtimeTpl, {
39-
enableSafeAreasSupport: Boolean(api.config?.reactNavigation?.enableSafeAreasSupport),
4039
loading:
4140
typeof dynamicImport === 'object' && typeof dynamicImport.loading === 'string'
4241
? `require('${dynamicImport.loading}').default`

packages/umi-preset-react-navigation/src/plugins/generateFiles/react-navigation/runtimeTpl.ts

-50
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,6 @@ export default `import React from 'react';
33
import {ApplyPluginsType, dynamic} from 'umi';
44
import {NavigationContainer, NavigationState} from '@react-navigation/native';
55
import {Navigation} from 'umi-renderer-react-navigation';
6-
import createNavigator from './createNavigator';
7-
{{#enableSafeAreasSupport}}
8-
import {SafeAreaProvider, SafeAreaView} from './exports';
9-
10-
export function patchRoutes({routes}) {
11-
if (Array.isArray(routes)) {
12-
for (const route of routes) {
13-
if (Array.isArray(route.wrappers)) {
14-
route.wrappers.push(SafeAreaView);
15-
} else {
16-
route.wrappers = [SafeAreaView];
17-
}
18-
}
19-
}
20-
console.log('patchRoutes:', routes);
21-
}
22-
{{/enableSafeAreasSupport}}
236
247
export function rootContainer(container, {plugin, routes, history}) {
258
return React.createElement(
@@ -46,37 +29,6 @@ export function rootContainer(container, {plugin, routes, history}) {
4629
async: true,
4730
});
4831
return () =>
49-
{{#enableSafeAreasSupport}}
50-
React.createElement(
51-
SafeAreaProvider,
52-
null,
53-
React.createElement(
54-
NavigationContainer,
55-
{
56-
theme: {{{ theme }}},
57-
initialState,
58-
onStateChange: (state?: NavigationState): void => {
59-
plugin.applyPlugins({
60-
key: 'onReactNavigationStateChange',
61-
type: ApplyPluginsType.event,
62-
initialValue: state,
63-
async: true,
64-
});
65-
},
66-
},
67-
React.createElement(Navigation, {
68-
routes,
69-
history,
70-
navigator: createNavigator(),
71-
plugin,
72-
{{#enableTitle}}
73-
defaultTitle: '{{{ defaultTitle }}}',
74-
{{/enableTitle}}
75-
}),
76-
),
77-
);
78-
{{/enableSafeAreasSupport}}
79-
{{^enableSafeAreasSupport}}
8032
React.createElement(
8133
NavigationContainer,
8234
{
@@ -94,14 +46,12 @@ export function rootContainer(container, {plugin, routes, history}) {
9446
React.createElement(Navigation, {
9547
routes,
9648
history,
97-
navigator: createNavigator(),
9849
plugin,
9950
{{#enableTitle}}
10051
defaultTitle: '{{{ defaultTitle }}}',
10152
{{/enableTitle}}
10253
}),
10354
);
104-
{{/enableSafeAreasSupport}}
10555
},
10656
}),
10757
);

packages/umi-renderer-react-navigation/README.md

+3-9
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@
22

33
针对 [react-navigation](https://reactnavigation.org/)[umi](https://umijs.org/) 渲染器。
44

5-
umi 默认渲染器: `@umijs/renderer-react` 使用[react-router](https://reacttraining.com/react-router/)渲染路由的代码逻辑无法替换
5+
开发者无须单独安装该依赖,需要和 <a href="https://github.com/xuyuanxiang/umi-react-native/tree/master/packages/umi-preset-react-navigation#readme" target="_blank">umi-preset-react-navigation</a> 配套使用,目前仅支持 Native,暂不考虑 Web
66

7-
只能替换整个`@umijs/renderer-react`,于是产生了这个项目。
7+
_umi 默认渲染器:`@umijs/renderer-react` 使用[react-router](https://reacttraining.com/react-router/)渲染路由。_
88

9-
## 如何使用 [umi](https://umijs.org/) 开发 RN 应用
10-
11-
请移步至:<a href="https://github.com/xuyuanxiang/umi-react-native#readme" target="_blank">umi-react-native</a>。
12-
13-
## 如何使用 [react-navigation](https://reactnavigation.org/) 替换 umi 默认的 [react-router](https://reacttraining.com/react-router/)
14-
15-
请移步至:<a href="https://github.com/xuyuanxiang/umi-react-native/tree/master/packages/umi-preset-react-navigation#readme" target="_blank">umi-preset-react-navigation</a>
9+
_了解如何使用[umi](https://umijs.org/)开发 RN 应用,请移步至:_<a href="https://github.com/xuyuanxiang/umi-react-native#readme" target="_blank">umi-react-native</a>

packages/umi-renderer-react-navigation/package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@
3636
"url": "https://github.com/xuyuanxiang/umi-react-native/issues"
3737
},
3838
"peerDependencies": {
39-
"@react-navigation/native": "^5.3.0",
39+
"react": ">=16.8.0",
40+
"react-native": ">=0.60.0",
4041
"umi": "^3.0.0"
4142
},
4243
"dependencies": {
44+
"@react-navigation/native": "5.3.0",
45+
"@react-navigation/routers": "5.4.4",
46+
"@react-navigation/stack": "5.3.3",
4347
"@umijs/renderer-react": "^3.0.0",
4448
"blueimp-md5": "2.15.0",
4549
"url-join": "4.0.1"

0 commit comments

Comments
 (0)