-
-
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.
docs(userequest demos): add demos of useRequest
- Loading branch information
Showing
19 changed files
with
409 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ page { | |
|
||
&__content { | ||
padding: 0 20px; | ||
position: relative; | ||
|
||
&.no-padding { | ||
padding: 0; | ||
|
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 缓存 & SWR', | ||
}; |
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,59 @@ | ||
/** | ||
* desc: 通过 `options.fetchKey` ,可以将请求进行分类,每一类的请求都有独立的状态,你可以在 `fetches` 中找到所有的请求。 | ||
*/ | ||
import React, { useState } from 'react'; | ||
import { useRequest } from 'taro-hooks'; | ||
import Mock from 'mockjs'; | ||
|
||
import { AtButton } from 'taro-ui'; | ||
import { View } from '@tarojs/components'; | ||
import DocPage from '@components/DocPage'; | ||
|
||
async function getArticle(): Promise<{ data: string; time: number }> { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve({ | ||
data: Mock.mock('@cparagraph(10, 20)'), | ||
time: new Date().getTime(), | ||
}); | ||
}, 1000); | ||
}); | ||
} | ||
|
||
const Article = () => { | ||
const { data, loading } = useRequest(getArticle, { | ||
cacheKey: 'article', | ||
}); | ||
if (!data && loading) { | ||
return <View>loading</View>; | ||
} | ||
return ( | ||
<> | ||
<View>Latest request time: {data?.time}</View> | ||
<View>{data?.data}</View> | ||
</> | ||
); | ||
}; | ||
|
||
const CacheKeyRequest = () => { | ||
const [state, setState] = useState<boolean>(false); | ||
|
||
return ( | ||
<DocPage title="useRequest 请求" panelTitle="缓存 & SWR"> | ||
<View> | ||
You can click the button multiple times, the article component will show | ||
the cached data first. | ||
</View> | ||
<AtButton | ||
type="primary" | ||
onClick={() => setState(!state)} | ||
customStyle={{ marginTop: '10px' }} | ||
> | ||
show/hidden | ||
</AtButton> | ||
{state && <Article />} | ||
</DocPage> | ||
); | ||
}; | ||
|
||
export default CacheKeyRequest; |
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 防抖', | ||
}; |
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,53 @@ | ||
/** | ||
* desc: 通过设置 `options.debounceInterval` ,则进入防抖模式。此时如果频繁触发 `run` ,则会以防抖策略进行请求。 | ||
*/ | ||
import React, { useEffect, useState } from 'react'; | ||
import { useRequest } from 'taro-hooks'; | ||
import Mock from 'mockjs'; | ||
|
||
import { AtInput, AtList, AtListItem } from 'taro-ui'; | ||
import { View } from '@tarojs/components'; | ||
import DocPage from '@components/DocPage'; | ||
|
||
async function getEmail(search: string): Promise<string[]> { | ||
console.log(search); | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(Mock.mock({ 'data|5': ['@email()'] }).data); | ||
}, 300); | ||
}); | ||
} | ||
|
||
const DebounceRequest = () => { | ||
const { data, loading, run } = useRequest(getEmail, { | ||
debounceInterval: 500, | ||
manual: true, | ||
}); | ||
|
||
const [value, setValue] = useState<string>(); | ||
|
||
useEffect(() => { | ||
value && run(value); | ||
}, [value, run]); | ||
|
||
return ( | ||
<DocPage title="useRequest 请求" panelTitle="防抖"> | ||
<View>Enter quickly to see the effect</View> | ||
<AtInput | ||
name="email" | ||
value={value} | ||
onChange={(e) => setValue(e as string)} | ||
placeholder="Select Emails" | ||
/> | ||
{loading ? ( | ||
<View>loading...</View> | ||
) : ( | ||
<AtList> | ||
{data && data.map((i: string) => <AtListItem key={i} title={i} />)} | ||
</AtList> | ||
)} | ||
</DocPage> | ||
); | ||
}; | ||
|
||
export default DebounceRequest; |
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 突变', | ||
}; |
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,46 @@ | ||
/** | ||
* desc: 你可以通过 `mutate` ,直接修改 `data` 。 `mutate` 函数参数可以为 `newData` 或 `(oldData)=> newData` 。 | ||
*/ | ||
import React, { useState } from 'react'; | ||
import { useRequest } from 'taro-hooks'; | ||
import Mock from 'mockjs'; | ||
|
||
import { AtInput, AtButton } from 'taro-ui'; | ||
import { View } from '@tarojs/components'; | ||
import DocPage from '@components/DocPage'; | ||
|
||
import 'taro-ui/dist/style/components/icon.scss'; | ||
|
||
function getUsername(): Promise<string> { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(Mock.mock('@name()')); | ||
}, 1000); | ||
}); | ||
} | ||
|
||
const MutateRequest = () => { | ||
const [state, setState] = useState(''); | ||
const { data, mutate, loading } = useRequest(getUsername, { | ||
onSuccess: (result: string) => { | ||
setState(result); | ||
}, | ||
}); | ||
|
||
return ( | ||
<DocPage title="useRequest 请求" panelTitle="突变"> | ||
<View>username: {data}</View> | ||
<AtInput | ||
name="username" | ||
onChange={(e) => setState(e as string)} | ||
value={state} | ||
placeholder="Please enter username" | ||
/> | ||
<AtButton type="primary" loading={loading} onClick={() => mutate(state)}> | ||
{loading ? 'loading' : 'Edit'} | ||
</AtButton> | ||
</DocPage> | ||
); | ||
}; | ||
|
||
export default MutateRequest; |
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,66 @@ | ||
/** | ||
* desc: 同一个 `cacheKey` 的请求,是全局共享的,也就是你可以提前加载数据。利用该特性,可以很方便的实现预加载。 | ||
*/ | ||
import React, { useEffect, useState } from 'react'; | ||
import { useRequest } from 'taro-hooks'; | ||
import Mock from 'mockjs'; | ||
|
||
import { AtButton } from 'taro-ui'; | ||
import { View } from '@tarojs/components'; | ||
import DocPage from '@components/DocPage'; | ||
|
||
async function getArticle(): Promise<{ data: string; time: number }> { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve({ | ||
data: Mock.mock('@cparagraph(10, 20)'), | ||
time: new Date().getTime(), | ||
}); | ||
}, 1000); | ||
}); | ||
} | ||
|
||
const Article = () => { | ||
const { data, loading } = useRequest(getArticle, { | ||
cacheKey: 'article', | ||
}); | ||
if (!data && loading) { | ||
return <View>loading</View>; | ||
} | ||
return ( | ||
<> | ||
<View>Latest request time: {data?.time}</View> | ||
<View>{data?.data}</View> | ||
</> | ||
); | ||
}; | ||
|
||
const PreloadRequest = () => { | ||
const [state, setState] = useState<boolean>(false); | ||
const { run } = useRequest(getArticle, { | ||
cacheKey: 'article', | ||
manual: true, | ||
}); | ||
|
||
useEffect(() => { | ||
if (!state) { | ||
run(); | ||
} | ||
}, [state, run]); | ||
|
||
return ( | ||
<DocPage title="useRequest 请求" panelTitle="预加载"> | ||
<View>When the article hidden, the article data is preloaded.</View> | ||
<AtButton | ||
type="primary" | ||
onClick={() => setState(!state)} | ||
customStyle={{ marginTop: '10px' }} | ||
> | ||
show/hidden | ||
</AtButton> | ||
{state && <Article />} | ||
</DocPage> | ||
); | ||
}; | ||
|
||
export default PreloadRequest; |
3 changes: 3 additions & 0 deletions
3
packages/app/src/pages/useRequest/refreshOnWindowFocus/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 屏幕聚焦重新请求', | ||
}; |
43 changes: 43 additions & 0 deletions
43
packages/app/src/pages/useRequest/refreshOnWindowFocus/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,43 @@ | ||
/** | ||
* desc: 只有当 `options.ready` 变为 true 时, 才会发起请求,基于该特性可以实现串行请求,依赖请求等。 | ||
*/ | ||
import React from 'react'; | ||
import { useRequest } from 'taro-hooks'; | ||
import Mock from 'mockjs'; | ||
|
||
import { View } from '@tarojs/components'; | ||
import { AtActivityIndicator } from 'taro-ui'; | ||
import DocPage from '@components/DocPage'; | ||
|
||
import 'taro-ui/dist/style/components/loading.scss'; | ||
|
||
function getUsername(): Promise<string> { | ||
const userInfo = Mock.mock('@name()'); | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(userInfo); | ||
}, 1000); | ||
}); | ||
} | ||
|
||
const RefreshOnWindowFocusRequest = () => { | ||
const { data, loading } = useRequest(getUsername, { | ||
refreshOnWindowFocus: true, | ||
}); | ||
|
||
return ( | ||
<DocPage title="useRequest 请求" panelTitle="屏幕聚焦重新请求"> | ||
<View> | ||
You can try to click elsewhere and click back to try. (Or hide the page | ||
and show it again) | ||
</View> | ||
<AtActivityIndicator | ||
mode="center" | ||
isOpened={loading} | ||
></AtActivityIndicator> | ||
<View>Username: {data}</View> | ||
</DocPage> | ||
); | ||
}; | ||
|
||
export default RefreshOnWindowFocusRequest; |
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 节流', | ||
}; |
Oops, something went wrong.