Skip to content

Commit 13c4051

Browse files
committed
feat(uselaunchoptions): add useLaunchOptions hooks
1 parent 3deae8b commit 13c4051

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed

packages/app/src/app.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default {
2020
// wechat
2121
'pages/useAPICheck/index',
2222
'pages/useUpdateManager/index',
23+
'pages/useLaunchOptions/index',
2324
// network
2425
'pages/useRequest/index',
2526
'pages/useRequest/defaultRequest/index',

packages/app/src/constant/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ export const ChildrenList: { [_: string]: APIChildrenItem[] } = {
8888
id: 'useUpdateManager',
8989
name: 'useUpdateManager 更新',
9090
},
91+
{
92+
id: 'useLaunchOptions',
93+
name: 'useLaunchOptions 启动参数',
94+
},
9195
],
9296
[APIChildrenName.network]: [
9397
{
@@ -104,3 +108,12 @@ export const ChildrenList: { [_: string]: APIChildrenItem[] } = {
104108
},
105109
],
106110
};
111+
112+
export const SceneEnum: { [_: number]: string } = {
113+
1020: '公众号 profile 页相关小程序列表',
114+
1035: '公众号自定义菜单',
115+
1036: 'App 分享消息卡片',
116+
1037: '小程序打开小程序',
117+
1038: '从另一个小程序返回',
118+
1043: '公众号模板消息',
119+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
navigationBarTitleText: 'useLaunchOptions',
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React from 'react';
2+
import { AtRadio, AtNoticebar } from 'taro-ui';
3+
import DocPage from '@components/DocPage';
4+
5+
import { General, ENV_TYPE } from '@tarojs/taro';
6+
import { useLaunchOptions, useEnv } from 'taro-hooks';
7+
8+
import 'taro-ui/dist/style/components/icon.scss';
9+
10+
import { SceneEnum } from '../../constant';
11+
12+
const MOCK = '1';
13+
14+
const transferOptions = (launchOptions: General.LaunchOptionsApp) =>
15+
Object.keys(launchOptions).map((key: any) => {
16+
let desc = JSON.stringify(
17+
launchOptions[key as keyof General.LaunchOptionsApp] || '',
18+
);
19+
20+
if (key === 'scene') {
21+
desc =
22+
desc +
23+
':' +
24+
(SceneEnum[launchOptions[key as keyof General.LaunchOptionsApp]] || '');
25+
}
26+
return {
27+
label: key + ':',
28+
value: key,
29+
desc,
30+
};
31+
});
32+
33+
export default () => {
34+
const launchOptions = useLaunchOptions();
35+
const env = useEnv();
36+
return (
37+
<>
38+
<AtNoticebar marquee>请尝试从不同场景打开小程序观察参数变化</AtNoticebar>
39+
<DocPage title="useLaunchOptions 启动参数" panelTitle="useLaunchOptions">
40+
{env !== ENV_TYPE.WEAPP && '改hook仅可在小程序中使用'}
41+
<AtRadio
42+
options={transferOptions(launchOptions)}
43+
value={MOCK}
44+
onClick={console.log}
45+
/>
46+
</DocPage>
47+
</>
48+
);
49+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
title: useLaunchOptions
3+
nav:
4+
title: Hooks
5+
path: /hooks
6+
order: 2
7+
group:
8+
title: 小程序
9+
path: /wechat
10+
---
11+
12+
# useLaunchOptions
13+
14+
获取小程序启动时参数(仅小程序端可用)
15+
16+
## 何时使用
17+
18+
当要对小程序启动时做参数判断时
19+
20+
## API
21+
22+
```jsx | pure
23+
const launchOptions = useLaunchOptions();
24+
```
25+
26+
## 返回值说明
27+
28+
| 返回值 | 说明 | 类型 |
29+
| ------------- | -------- | ------------------ |
30+
| launchOptions | 启动参数 | `LaunchAppOptions` |
31+
32+
## 代码演示
33+
34+
<code src="@pages/useLaunchOptions" />
35+
36+
## Hook 支持度
37+
38+
| 微信小程序 | H5 | ReactNative |
39+
| :--------: | :-: | :---------: |
40+
| ✔️ | | |
41+
42+
## FAQ
43+
44+
### 1. 更多说明
45+
46+
[小程序相关文档](https://developers.weixin.qq.com/miniprogram/dev/api/base/app/life-cycle/wx.getLaunchOptionsSync.html)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ENV_TYPE, getLaunchOptionsSync, General } from '@tarojs/taro';
2+
import useEnv from '../useEnv';
3+
4+
function useLaunchOptions(): General.LaunchOptionsApp | {} {
5+
const env = useEnv();
6+
return env === ENV_TYPE.WEAPP ? getLaunchOptionsSync() : {};
7+
}
8+
9+
export default useLaunchOptions;

0 commit comments

Comments
 (0)