-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(faq & useapp): add useApp hooks & faq of useSelectorQuery
- Loading branch information
Showing
16 changed files
with
195 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default { | ||
navigationBarTitleText: 'useApp', | ||
enableShareAppMessage: true, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React, { useCallback, useEffect } from 'react'; | ||
import { AtList, AtListItem, AtMessage, AtNoticebar } from 'taro-ui'; | ||
import DocPage from '@components/DocPage'; | ||
|
||
import { useModal, useApp } from 'taro-hooks'; | ||
|
||
import MOCK from 'mockjs'; | ||
|
||
export default () => { | ||
const [appInstance, globalData, setGlobalData] = useApp(true); | ||
const [show] = useModal({ | ||
title: 'useApp', | ||
content: '您是否要随机修改当前全局变量', | ||
mask: true, | ||
}); | ||
|
||
useEffect(() => { | ||
show({ | ||
content: `当前获取示例(${ | ||
appInstance?.config?.window?.navigationBarTitleText || '' | ||
}): 页面个数(${appInstance?.config?.pages.length || 0})`, | ||
}); | ||
}, [show, appInstance]); | ||
|
||
const handleItemClick = useCallback( | ||
(key: string) => { | ||
show().then((res: any) => { | ||
if (res.confirm) { | ||
setGlobalData(key, MOCK.mock('@name()')); | ||
} | ||
}); | ||
}, | ||
[setGlobalData, show], | ||
); | ||
|
||
return ( | ||
<> | ||
<AtNoticebar marquee> | ||
文档中的用例非完整应用, 可查看h5 或 小程序 demo 查看效果 | ||
</AtNoticebar> | ||
<AtMessage /> | ||
<DocPage title="useApp 应用实例" panelTitle="useApp"> | ||
<AtList> | ||
{Object.entries(globalData).map(([key, value]) => ( | ||
<AtListItem | ||
onClick={() => handleItemClick(key)} | ||
key={key} | ||
title={key} | ||
note={JSON.stringify(value)} | ||
/> | ||
))} | ||
</AtList> | ||
</DocPage> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
title: useApp | ||
nav: | ||
title: Hooks | ||
path: /hooks | ||
order: 2 | ||
group: | ||
title: 基础 | ||
path: /basic | ||
--- | ||
|
||
# useApp | ||
|
||
获取当前程序的唯一实例以及全局数据 | ||
|
||
## 何时使用 | ||
|
||
当需要获取程序实例或操作全局数据时 | ||
|
||
## API | ||
|
||
```jsx | pure | ||
const [appInstance, globalData, setGlobalData] = useApp(allowDefault?: boolean) | ||
``` | ||
|
||
## 参数 | ||
|
||
| 参数 | 说明 | 类型 | 默认值 | | ||
| ------------ | --------------------------------------------------------------------------------------------------------- | --------- | ------ | | ||
| allowDefault | 在 App 未定义时返回默认实现。当 App 被调用时,默认实现中定义的属性会被覆盖合并到 App 中。一般用于独立分包 | `Boolean` | - | | ||
|
||
## 返回值说明 | ||
|
||
| 返回值 | 说明 | 类型 | | ||
| ------------- | ---------------------------- | ------------------------------------------------------------------ | | ||
| appInstance | 小程序全局唯一 APP 实例 | `Instance` | | ||
| globalData | 全局数据(须在 app.ts 中定义) | `TRecord` | | ||
| setGlobalData | 动态修改全局数据 | `(key: string, value: unknown) => Promise<General.CallbackResult>` | | ||
|
||
## 代码演示 | ||
|
||
<code src="@pages/useApp" /> | ||
|
||
## Hook 支持度 | ||
|
||
| 微信小程序 | H5 | ReactNative | | ||
| :--------: | :-: | :---------: | | ||
| ✔️ | ✔️ | ✔️ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useCallback, useEffect, useState, useRef } from 'react'; | ||
import { getApp } from '@tarojs/taro'; | ||
|
||
import { isUndefined } from '../utils/tool'; | ||
|
||
import type { App, General } from '@tarojs/taro'; | ||
import type { TRecord } from '../../typings'; | ||
|
||
export type TSetGlobalData = ( | ||
key: string, | ||
value: unknown, | ||
) => Promise<General.CallbackResult>; | ||
|
||
function useApp( | ||
allDefault?: boolean, | ||
): [AppInstance: App, globalData: TRecord, setGlobalData: TSetGlobalData] { | ||
const [globalData, setGlobalData] = useState<TRecord>({}); | ||
const appInstance = useRef<App>( | ||
getApp({ allowDefault: Boolean(allDefault) }), | ||
); | ||
|
||
useEffect(() => { | ||
if (appInstance.current?.$app) { | ||
setGlobalData(appInstance.current.$app.globalData || {}); | ||
} | ||
}, [appInstance]); | ||
|
||
const setGlobalDataAsync = useCallback<TSetGlobalData>( | ||
(key, value) => { | ||
return new Promise((resolve, reject) => { | ||
if (isUndefined(key)) { | ||
reject({ errMsg: 'setGlobalData: fail' }); | ||
} else { | ||
try { | ||
const prevGlobalData = { ...globalData }; | ||
prevGlobalData[key] = value; | ||
appInstance.current.$app.globalData = prevGlobalData; | ||
setGlobalData(prevGlobalData); | ||
resolve({ errMsg: 'setGlobalData: ok' }); | ||
} catch (e) { | ||
reject({ errMsg: 'setGlobalData: fail', data: e }); | ||
} | ||
} | ||
}); | ||
}, | ||
[globalData, appInstance], | ||
); | ||
|
||
return [appInstance.current, globalData, setGlobalDataAsync]; | ||
} | ||
|
||
export default useApp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3e0ebea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
taro-hooks – ./
taro-hooks-innocces.vercel.app
taro-hooks-git-main-innocces.vercel.app
taro-hooks-theta.vercel.app
3e0ebea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
taro-hooks-h5 – ./
taro-hooks-h5-green.vercel.app
taro-hooks-h5-innocces.vercel.app
taro-hooks-h5-git-main-innocces.vercel.app