-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement(alias & demos): add alias @components and add useRequest …
…demos
- Loading branch information
Showing
25 changed files
with
222 additions
and
44 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
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
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
3 changes: 3 additions & 0 deletions
3
packages/app/src/pages/useRequest/defaultRequest/index.config.ts
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 默认用法', | ||
}; |
35 changes: 35 additions & 0 deletions
35
packages/app/src/pages/useRequest/defaultRequest/index.tsx
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 @@ | ||
/** | ||
* desc: 在这个例子中, useRequest 接收了一个异步函数 `getUsername` ,在组件初次加载时, 自动触发该函数执行。同时 useRequest 会自动管理异步请求的 `loading` , `data` , `error` 等状态。 | ||
*/ | ||
import React from 'react'; | ||
import Mock from 'mockjs'; | ||
import { useRequest } from 'taro-hooks'; | ||
|
||
import { View } from '@tarojs/components'; | ||
import DocPage from '@components/DocPage'; | ||
|
||
function getUsername(): Promise<string> { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
// 兼容小程序写法 | ||
resolve(Mock.mock('@name()')); | ||
}, 1000); | ||
}); | ||
} | ||
|
||
const DefaultRequest = () => { | ||
const { data, error, loading } = useRequest(getUsername); | ||
return ( | ||
<DocPage title="useRequest 请求" panelTitle="默认请求"> | ||
{error ? ( | ||
<View>failed to load</View> | ||
) : loading ? ( | ||
'loading...' | ||
) : ( | ||
<View>Username: {data}</View> | ||
)} | ||
</DocPage> | ||
); | ||
}; | ||
|
||
export default DefaultRequest; |
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,44 +1,51 @@ | ||
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 { navigateTo } from '@tarojs/taro'; | ||
|
||
import { AtList, AtListItem } from 'taro-ui'; | ||
import DocPage from '@components/DocPage'; | ||
|
||
import 'taro-ui/dist/style/components/icon.scss'; | ||
import { AtListItemProps } from 'taro-ui/types/list'; | ||
|
||
export default () => { | ||
const onCheckForUpdate = useCallback((res) => { | ||
Taro.atMessage({ | ||
message: res.hasUpdate ? '有新版本' : '无新版本', | ||
}); | ||
}, []); | ||
interface IListItem extends AtListItemProps { | ||
route: string; | ||
} | ||
|
||
const onUpdateReady = useCallback(() => { | ||
Taro.atMessage({ | ||
message: '检查更新成功', | ||
}); | ||
}, []); | ||
const list: IListItem[] = [ | ||
{ | ||
title: '默认请求', | ||
note: '在组件初次加载时, 自动触发该函数执行。', | ||
route: 'defaultRequest', | ||
}, | ||
{ | ||
title: '手动触发', | ||
note: '通过设置 options.manual = true , 则需要手动调用 run 时才会触发执行异步函数。', | ||
route: 'manual', | ||
}, | ||
{ | ||
title: '轮询', | ||
note: '通过设置 options.pollingInterval,进入轮询模式,定时触发函数执行。', | ||
route: 'polling', | ||
}, | ||
]; | ||
|
||
const onUpdateFailed = useCallback(() => { | ||
Taro.atMessage({ | ||
message: '检查更新失败', | ||
}); | ||
export default () => { | ||
const routePage = useCallback((route: string) => { | ||
navigateTo({ url: `${route}/index` }); | ||
}, []); | ||
|
||
const updateManager = useUpdateManager({ | ||
onCheckForUpdate, | ||
onUpdateReady, | ||
onUpdateFailed, | ||
}); | ||
|
||
return ( | ||
<> | ||
<AtMessage /> | ||
<DocPage title="useRequest 请求" panelTitle="useRequest"> | ||
<View>检查更新中....</View> | ||
</DocPage> | ||
</> | ||
<DocPage title="useRequest 请求"> | ||
<AtList> | ||
{list.map((listItem, index) => ( | ||
<AtListItem | ||
{...listItem} | ||
key={index} | ||
onClick={() => routePage(listItem.route)} | ||
/> | ||
))} | ||
</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
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,52 @@ | ||
/** | ||
* desc: 通过设置 `options.manual = true` , 则需要手动调用 `run` 时才会触发执行异步函数。 | ||
*/ | ||
import React, { useState } from 'react'; | ||
import { useRequest } from 'taro-hooks'; | ||
import Taro from '@tarojs/taro'; | ||
|
||
import { AtMessage, AtInput, AtButton } from 'taro-ui'; | ||
import DocPage from '@components/DocPage'; | ||
|
||
type Result = { success: boolean }; | ||
|
||
function changeUsername(username: string): Promise<Result> { | ||
console.log(username); | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve({ success: true }); | ||
}, 1000); | ||
}); | ||
} | ||
|
||
const DefaultRequest = () => { | ||
const [state, setState] = useState(''); | ||
|
||
const { loading, run } = useRequest(changeUsername, { | ||
manual: true, | ||
onSuccess: (result: Result, params: string[]) => { | ||
if (result.success) { | ||
setState(''); | ||
Taro.atMessage({ | ||
message: `The username was changed to "${params[0]}" !`, | ||
}); | ||
} | ||
}, | ||
}); | ||
return ( | ||
<DocPage title="useRequest 请求" panelTitle="手动触发"> | ||
<AtMessage /> | ||
<AtInput | ||
name="username" | ||
onChange={(e) => setState(e as string)} | ||
value={state} | ||
placeholder="Please enter username" | ||
/> | ||
<AtButton type="primary" loading={loading} onClick={() => run(state)}> | ||
{loading ? 'loading' : 'Edit'} | ||
</AtButton> | ||
</DocPage> | ||
); | ||
}; | ||
|
||
export default DefaultRequest; |
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,43 @@ | ||
/** | ||
* desc: | | ||
* 通过设置 `options.pollingInterval` ,进入轮询模式,定时触发函数执行。 | ||
* | ||
* - 通过设置 `options.pollingWhenHidden=false` ,在屏幕不可见时,暂时暂停定时任务。 | ||
* - 通过 `run` / `cancel` 来 开启/停止 轮询。 | ||
* - 在 `options.manual=true` 时,需要第一次执行 `run` 后,才开始轮询。 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { useRequest } from 'taro-hooks'; | ||
import Mock from 'mockjs'; | ||
|
||
import { View } from '@tarojs/components'; | ||
import DocPage from '@components/DocPage'; | ||
import { AtButton } from 'taro-ui'; | ||
|
||
function getUsername(): Promise<string> { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(Mock.mock('@name()')); | ||
}, 1000); | ||
}); | ||
} | ||
|
||
export default () => { | ||
const { data, loading, run, cancel } = useRequest(getUsername, { | ||
pollingInterval: 1000, | ||
pollingWhenHidden: false, | ||
}); | ||
|
||
return ( | ||
<DocPage title="useRequest 请求" panelTitle="轮询"> | ||
<View>Username: {loading ? 'loading' : data}</View> | ||
<AtButton type="primary" onClick={run} customStyle={{ margin: '10px 0' }}> | ||
start | ||
</AtButton> | ||
<AtButton type="secondary" onClick={cancel}> | ||
stop | ||
</AtButton> | ||
</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
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
Oops, something went wrong.