-
-
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.
docs(userequest): add custom request to docs
- Loading branch information
Showing
12 changed files
with
255 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
navigationBarTitleText: 'useRequest requstMethod', | ||
}; |
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,41 @@ | ||
/** | ||
* desc: 通过设置 `requstMethod`, 可以使用自己的请求库。 | ||
*/ | ||
import React from 'react'; | ||
import { useRequest } from 'taro-hooks'; | ||
import axios from 'axios/dist/axios.min.js'; | ||
import adapter from 'axios-miniprogram-adapter'; | ||
|
||
import { View } from '@tarojs/components'; | ||
import DocPage from '@components/DocPage'; | ||
|
||
const requestMethod = axios.create( | ||
process.env.TARO_ENV === 'h5' | ||
? {} | ||
: { | ||
adapter, | ||
}, | ||
); | ||
|
||
const MethodRequest = () => { | ||
const { data, error, loading } = useRequest( | ||
'https://run.mocky.io/v3/cd8e3926-740c-4a2c-ab80-45c0c4fe8dd2', | ||
{ | ||
requestMethod: (param: any) => requestMethod(param), | ||
}, | ||
); | ||
|
||
return ( | ||
<DocPage title="useRequest requstMethod" panelTitle="requstMethod"> | ||
<View> | ||
{error | ||
? 'failed to load' | ||
: loading | ||
? 'loading...' | ||
: 'responese:' + JSON.stringify(data?.data)} | ||
</View> | ||
</DocPage> | ||
); | ||
}; | ||
|
||
export default MethodRequest; |
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,28 @@ | ||
/** | ||
* desc: 如果 useRequest 第一个参数是 `string`、`object` 或 `()=> string|object`,则默认使用 Taro.request 发送网络请求 | ||
*/ | ||
import React from 'react'; | ||
import { useRequest } from 'taro-hooks'; | ||
|
||
import { View } from '@tarojs/components'; | ||
import DocPage from '@components/DocPage'; | ||
|
||
const FetchRequest = () => { | ||
const { data, error, loading } = useRequest( | ||
'https://run.mocky.io/v3/cd8e3926-740c-4a2c-ab80-45c0c4fe8dd2', | ||
); | ||
|
||
return ( | ||
<DocPage title="useRequest 默认请求库" panelTitle="默认请求库"> | ||
<View> | ||
{error | ||
? 'failed to load' | ||
: loading | ||
? 'loading...' | ||
: 'responese:' + JSON.stringify(data?.data)} | ||
</View> | ||
</DocPage> | ||
); | ||
}; | ||
|
||
export default FetchRequest; |
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/loadingDelay/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 Loading Delay', | ||
}; |
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,57 @@ | ||
/** | ||
* desc: 通过设置 `options.loadingDelay` ,可以延迟 `loading` 变成 `true` 的时间,有效防止闪烁。 | ||
*/ | ||
import React from 'react'; | ||
import { useRequest } from 'taro-hooks'; | ||
|
||
import { AtButton } from 'taro-ui'; | ||
import { View } from '@tarojs/components'; | ||
import DocPage from '@components/DocPage'; | ||
|
||
async function getCurrentTime(): Promise<number> { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(new Date().getTime()); | ||
}, 100); | ||
}); | ||
} | ||
|
||
const LoadingDelayRequest = () => { | ||
const getTimeAction = useRequest(getCurrentTime); | ||
|
||
const withLoadingDelayAction = useRequest(getCurrentTime, { | ||
loadingDelay: 200, | ||
}); | ||
|
||
const trigger = () => { | ||
getTimeAction.run(); | ||
withLoadingDelayAction.run(); | ||
}; | ||
|
||
return ( | ||
<DocPage title="useRequest loading Delay" panelTitle="loading Delay"> | ||
<View> | ||
loadingDelay can set delay loading, which can effectively prevent | ||
loading from flickering. | ||
</View> | ||
<AtButton | ||
type="primary" | ||
onClick={trigger} | ||
customStyle={{ margin: '10px 0' }} | ||
> | ||
run | ||
</AtButton> | ||
<View style={{ marginBottom: '10px' }}> | ||
Current Time: {getTimeAction.loading ? 'loading' : getTimeAction.data} | ||
</View> | ||
<View> | ||
Current Time:{' '} | ||
{withLoadingDelayAction.loading | ||
? 'loading' | ||
: withLoadingDelayAction.data} | ||
</View> | ||
</DocPage> | ||
); | ||
}; | ||
|
||
export default LoadingDelayRequest; |
3 changes: 3 additions & 0 deletions
3
packages/app/src/pages/useRequest/refreshDeps/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 refreshDeps', | ||
}; |
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,66 @@ | ||
/** | ||
* desc: 当 `options.refreshDeps` 变化时,useRequest 会使用之前的参数重新执行 service。 | ||
*/ | ||
import React, { useState } from 'react'; | ||
import { useRequest } from 'taro-hooks'; | ||
|
||
import { AtButton, AtActionSheet, AtActionSheetItem } from 'taro-ui'; | ||
import { View } from '@tarojs/components'; | ||
import DocPage from '@components/DocPage'; | ||
|
||
const userSchool = (id: string) => { | ||
switch (id) { | ||
case '1': | ||
return 'Tsinghua University'; | ||
case '2': | ||
return 'Beijing University'; | ||
case '3': | ||
return 'Zhejiang University'; | ||
default: | ||
return ''; | ||
} | ||
}; | ||
|
||
async function getUserSchool(userId: string): Promise<string> { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(userSchool(userId)); | ||
}, 1000); | ||
}); | ||
} | ||
|
||
const RefreshDepsRequest = () => { | ||
const [userId, setUserId] = useState('1'); | ||
const [isOpened, changeIsOpened] = useState<boolean>(false); | ||
const { data, loading } = useRequest(() => getUserSchool(userId), { | ||
refreshDeps: [userId], | ||
}); | ||
|
||
return ( | ||
<DocPage title="useRequest refreshDeps" panelTitle="refreshDeps"> | ||
<AtButton | ||
type="primary" | ||
onClick={() => changeIsOpened(true)} | ||
customStyle={{ margin: '10px 0' }} | ||
> | ||
点击更改userId: {userId} | ||
</AtButton> | ||
<View>School: {loading ? 'loading' : data}</View> | ||
<AtActionSheet isOpened={isOpened} onClose={() => changeIsOpened(false)}> | ||
{['1', '2', '3'].map((v) => ( | ||
<AtActionSheetItem | ||
onClick={() => { | ||
setUserId(v); | ||
changeIsOpened(false); | ||
}} | ||
key={v} | ||
> | ||
user {v} | ||
</AtActionSheetItem> | ||
))} | ||
</AtActionSheet> | ||
</DocPage> | ||
); | ||
}; | ||
|
||
export default RefreshDepsRequest; |
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