-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(usenetwork/useonline/usevisible): add useNetworkType useOnline u…
…seVisible hooks
- Loading branch information
Showing
25 changed files
with
507 additions
and
6 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
Binary file not shown.
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
|
@@ -24,3 +24,7 @@ | |
.iconfont-wechat:before { | ||
content: '\e609'; | ||
} | ||
|
||
.iconfont-network:before { | ||
content: '\e64c'; | ||
} |
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,3 @@ | ||
export default { | ||
navigationBarTitleText: 'useNetworkType', | ||
}; |
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,50 @@ | ||
import React, { useCallback, useEffect } from 'react'; | ||
import { AtMessage, AtRadio, AtNoticebar } from 'taro-ui'; | ||
import DocPage from '../../components/DocPage'; | ||
|
||
import { useNetworkType } from 'taro-hooks'; | ||
import Taro from '@tarojs/taro'; | ||
|
||
import 'taro-ui/dist/style/components/icon.scss'; | ||
|
||
const networkGroup = ['wifi', '2g', '3g', '4g', '5g', 'unknown', 'none']; | ||
|
||
export default () => { | ||
const networkType = useNetworkType(); | ||
|
||
const computedNetworkOption = useCallback(() => { | ||
return networkGroup.map((type) => ({ | ||
label: type, | ||
value: type, | ||
desc: '网络类型: ' + type, | ||
disabled: type !== networkType, | ||
})); | ||
}, [networkType]); | ||
|
||
const handleRadioClick = useCallback((type) => { | ||
Taro.atMessage({ | ||
message: '当前网络类型为: ' + type, | ||
type: 'info', | ||
}); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (networkType) handleRadioClick(networkType); | ||
}, [handleRadioClick, networkType]); | ||
|
||
return ( | ||
<> | ||
<AtNoticebar marquee> | ||
当前hook自动监听网络类型变化。可尝试更改设备网络类型测试。 | ||
</AtNoticebar> | ||
<AtMessage /> | ||
<DocPage title="useNetworkType 网络类型" panelTitle="useNetworkType"> | ||
<AtRadio | ||
options={computedNetworkOption()} | ||
value={networkType} | ||
onClick={(value) => handleRadioClick(value)} | ||
/> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
navigationBarTitleText: 'useOnline', | ||
}; |
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,35 @@ | ||
import React, { useCallback, useEffect } from 'react'; | ||
import { AtMessage, AtSwitch, AtNoticebar } from 'taro-ui'; | ||
import DocPage from '../../components/DocPage'; | ||
|
||
import { useOnline } from 'taro-hooks'; | ||
import Taro from '@tarojs/taro'; | ||
|
||
import 'taro-ui/dist/style/components/icon.scss'; | ||
|
||
export default () => { | ||
const online = useOnline(); | ||
|
||
const handleOnlineStatusChange = useCallback((status) => { | ||
Taro.atMessage({ | ||
message: '当前网络状态为: ' + (status ? '开启' : '关闭'), | ||
type: 'info', | ||
}); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (typeof online === 'boolean') handleOnlineStatusChange(online); | ||
}, [online, handleOnlineStatusChange]); | ||
|
||
return ( | ||
<> | ||
<AtNoticebar marquee> | ||
当前hook自动监听网络状态变化。可尝试更改设备网络状态测试。 | ||
</AtNoticebar> | ||
<AtMessage /> | ||
<DocPage title="useOnline 网络状态" panelTitle="useOnline"> | ||
<AtSwitch title="当前网络状态" checked={online} disabled /> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
navigationBarTitleText: 'useRequest', | ||
}; |
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,44 @@ | ||
import React, { useCallback } from 'react'; | ||
import { AtMessage } from 'taro-ui'; | ||
import { View } from '@tarojs/components'; | ||
import DocPage from '../../components/DocPage'; | ||
|
||
import { useUpdateManager } from 'taro-hooks'; | ||
import Taro from '@tarojs/taro'; | ||
|
||
import 'taro-ui/dist/style/components/icon.scss'; | ||
|
||
export default () => { | ||
const onCheckForUpdate = useCallback((res) => { | ||
Taro.atMessage({ | ||
message: res.hasUpdate ? '有新版本' : '无新版本', | ||
}); | ||
}, []); | ||
|
||
const onUpdateReady = useCallback(() => { | ||
Taro.atMessage({ | ||
message: '检查更新成功', | ||
}); | ||
}, []); | ||
|
||
const onUpdateFailed = useCallback(() => { | ||
Taro.atMessage({ | ||
message: '检查更新失败', | ||
}); | ||
}, []); | ||
|
||
const updateManager = useUpdateManager({ | ||
onCheckForUpdate, | ||
onUpdateReady, | ||
onUpdateFailed, | ||
}); | ||
|
||
return ( | ||
<> | ||
<AtMessage /> | ||
<DocPage title="useRequest 请求" panelTitle="useRequest"> | ||
<View>检查更新中....</View> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
navigationBarTitleText: 'useVisible', | ||
}; |
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,37 @@ | ||
import React, { useEffect, useState, useContext } from 'react'; | ||
import { AtNoticebar, AtTimeline } from 'taro-ui'; | ||
import { AtTimelineProps } from 'taro-ui/types/timeline'; | ||
|
||
import DocPage from '../../components/DocPage'; | ||
|
||
import { useVisible } from 'taro-hooks'; | ||
|
||
import 'taro-ui/dist/style/components/icon.scss'; | ||
|
||
export default () => { | ||
const visible = useVisible(); | ||
const [statusStack, setStatusStack] = useState<AtTimelineProps['items']>([]); | ||
|
||
useEffect(() => { | ||
setStatusStack((prevStack) => [ | ||
...prevStack, | ||
{ | ||
title: visible ? '页面显示' : '页面隐藏', | ||
content: ['时间: ' + Date.now()], | ||
color: visible ? 'green' : 'red', | ||
icon: visible ? 'clock' : 'blocked', | ||
}, | ||
]); | ||
}, [visible]); | ||
|
||
return ( | ||
<> | ||
<AtNoticebar marquee> | ||
当前hook自动监听页面状态变化。可尝试打开小程序设置或切换后台测试。 | ||
</AtNoticebar> | ||
<DocPage title="useVisible 页面状态" panelTitle="useVisible"> | ||
<AtTimeline items={statusStack} /> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,30 @@ | ||
// env | ||
import useEnv from './useEnv'; | ||
import useAPICheck from './useAPICheck'; | ||
|
||
// basic | ||
import useBase64ToArrayBuffer from './useBase64ToArrayBuffer'; | ||
import useArrayBufferToBase64 from './useArrayBufferToBase64'; | ||
import useSystemInfo from './useSystemInfo'; | ||
import useEvent from './useEvent'; | ||
|
||
import useAPICheck from './useAPICheck'; | ||
import useUpdateManager from './useUpdateManager'; | ||
|
||
// network | ||
import useNetworkType from './useNetworkType'; | ||
import useOnline from './useOnline'; | ||
import useRequest from './useRequest'; | ||
|
||
export { | ||
useEnv, | ||
useAPICheck, | ||
useBase64ToArrayBuffer, | ||
useArrayBufferToBase64, | ||
useSystemInfo, | ||
useEvent, | ||
useAPICheck, | ||
useUpdateManager, | ||
useNetworkType, | ||
useOnline, | ||
useRequest, | ||
}; |
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,54 @@ | ||
--- | ||
title: useNetworkType | ||
nav: | ||
title: Hooks | ||
path: /hooks | ||
order: 2 | ||
group: | ||
title: 网络 | ||
path: /network | ||
--- | ||
|
||
<Alert>由于获取网络信息为异步, 故初始值为<code>undefined</code>。若业务中可做初始判空处理。 此外改 hook 会自动监听当前环境 networkType</Alert> | ||
|
||
# useNetworkType | ||
|
||
获取网络类型 | ||
|
||
## 何时使用 | ||
|
||
当需要根据当前网络类型做判断时 | ||
|
||
## API | ||
|
||
```jsx | pure | ||
const networkType: NetworkType = useNetworkType(); | ||
``` | ||
|
||
## 返回值说明 | ||
|
||
| 返回值 | 说明 | 类型 | | ||
| ----------- | ------------ | ------------- | | ||
| networkType | 当前网络类型 | `NetworkType` | | ||
|
||
### NetworkType | ||
|
||
| 类型 | 值 | | ||
| ------- | ------- | | ||
| wifi | wifi | | ||
| 2g | 2g | | ||
| 3g | 3g | | ||
| 4g | 4g | | ||
| 5g | 5g | | ||
| unknown | unknown | | ||
| none | none | | ||
|
||
## 代码演示 | ||
|
||
<code src="@pages/useNetworkType" /> | ||
|
||
## Hook 支持度 | ||
|
||
| 微信小程序 | H5 | ReactNative | | ||
| :--------: | :-: | :---------: | | ||
| ✔️ | ✔️ | ✔️ | |
Oops, something went wrong.