Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(useRequest): add note for refreshDeps and update demos #2408

Merged
merged 2 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 15 additions & 12 deletions packages/hooks/src/useRequest/doc/refreshDeps/demo/refreshDeps.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
/**
* title: Repeat last request
* desc: When the dependency array changes, use the previous parameters to make the request again.
*
* title.zh-CN: 重复上一次请求
* desc.zh-CN: 依赖数组变化时,使用上一次的参数重新发起请求。
*/

import React, { useState } from 'react';
import Mock from 'mockjs';
import { Space, Button } from 'antd';
import { useRequest } from 'ahooks';

function getUsername(id: number): Promise<string> {
console.log('use-request-refresh-deps-id', id);
console.log('getUsername id:', id);

return new Promise((resolve) => {
setTimeout(() => {
Expand All @@ -18,17 +27,11 @@ export default () => {
refreshDeps: [userId],
});

if (loading) {
return <div>loading...</div>;
}

return (
<div>
<p>Username: {data}</p>
<button style={{ marginRight: '8px' }} onClick={() => setUserId(Math.random())}>
Use previous id to refresh
</button>
<button onClick={() => run(Math.random())}>Use latest id to refresh</button>
</div>
<Space direction="vertical">
<p>Username: {loading ? 'loading...' : data}</p>
<Button onClick={() => setUserId(Math.random())}>Use previous id to refresh</Button>
<Button onClick={() => run(Math.random())}>Use latest id to refresh</Button>
</Space>
);
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
/**
* title: Custom refresh
* desc: This example shows that when the dependency array changes, it checks the parameters' validity first and then makes a new request.
*
* title.zh-CN: 自定义刷新行为
* desc.zh-CN: 该示例展示了当依赖数组变化时,首先校验参数合法性,然后发起新的请求。
*/

import React, { useState } from 'react';
import Mock from 'mockjs';
import { isNumber } from 'lodash-es';
import { Button, Space } from 'antd';
import { useRequest } from 'ahooks';

function getUsername(id: number): Promise<string> {
console.log('use-request-refresh-deps-id', id);
console.log('getUsername id:', id);

return new Promise((resolve) => {
setTimeout(() => {
Expand All @@ -16,20 +26,25 @@ export default () => {
const [userId, setUserId] = useState<number>();
const { data, loading, run } = useRequest((id: number) => getUsername(id), {
refreshDeps: [userId],
refreshDepsAction: () => run(userId),
refreshDepsAction: () => {
if (!isNumber(userId)) {
console.log(
`parameter "userId" expected to be a number, but got ${typeof userId}.`,
userId,
);
return;
}
run(userId);
},
});

if (loading) {
return <div>loading...</div>;
}

return (
<div>
<p>Username: {data}</p>
<button style={{ marginRight: '8px' }} onClick={() => setUserId(Math.random())}>
Use latest id to refresh
</button>
<button onClick={() => run(Math.random())}>Use latest id to refresh</button>
</div>
<Space direction="vertical">
<p>Username: {loading ? 'loading...' : data}</p>
<Button onClick={() => setUserId(Math.random())}>
Use latest id to refresh (by `refreshDeps`)
</Button>
<Button onClick={() => run(Math.random())}>Use latest id to refresh (by `run`)</Button>
</Space>
);
};
16 changes: 10 additions & 6 deletions packages/hooks/src/useRequest/doc/refreshDeps/refresyDeps.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ group:

# RefreshDeps

By setting `options.refreshDeps`, `useRequest` will run [refresh](https://ahooks.js.org/hooks/use-request/basic/#result) automatically when initialization and dependencies changes, achieving the effect of [Refresh (repeat the last request)](https://ahooks.js.org/hooks/use-request/basic/#refresh-repeat-the-last-request).
By setting `options.refreshDeps`, `useRequest` will run [refresh](https://ahooks.js.org/hooks/use-request/basic/#result) automatically when dependencies change, achieving the effect of [Refresh (repeat the last request)](https://ahooks.js.org/hooks/use-request/basic/#refresh-repeat-the-last-request).

```tsx | pure
const [userId, setUserId] = useState('1');
Expand All @@ -29,7 +29,7 @@ useEffect(() => {
}, [userId]);
```

### Refresh last request
### Repeat last request

<code src="./demo/refreshDeps.tsx" />

Expand All @@ -41,7 +41,11 @@ useEffect(() => {

### Options

| Property | Description | Type | Default |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------ | ---------------------- | ------- |
| refreshDeps | When the content of the array changes, trigger refresh. | `React.DependencyList` | `[]` |
| refreshDepsAction | Customize the request behavior for dependency refresh, this parameter is called after initialization and dependencies changes. | `() => void` | - |
| Property | Description | Type | Default |
| ----------------- | ------------------------------------------------------------------------------------------------------------- | ---------------------- | ------- |
| refreshDeps | When the content of the array changes, trigger refresh. | `React.DependencyList` | `[]` |
| refreshDepsAction | Customize the request behavior during dependency refresh; this parameter is invoked when dependencies change. | `() => void` | - |

## Remark

- If you set `options.manual = true`, both `refreshDeps` and `refreshDepsAction` are no longer effective, you need to trigger the request by `run/runAsync`.
16 changes: 10 additions & 6 deletions packages/hooks/src/useRequest/doc/refreshDeps/refresyDeps.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ group:

# 依赖刷新

通过设置 `options.refreshDeps`,在初始化和依赖变化时, `useRequest` 会自动调用 [refresh](https://ahooks.js.org/zh-CN/hooks/use-request/basic/#result) 方法,实现[刷新(重复上一次请求)](https://ahooks.js.org/zh-CN/hooks/use-request/basic/#刷新重复上一次请求)的效果。
通过设置 `options.refreshDeps`,在依赖变化时, `useRequest` 会自动调用 [refresh](https://ahooks.js.org/zh-CN/hooks/use-request/basic/#result) 方法,实现[刷新(重复上一次请求)](https://ahooks.js.org/zh-CN/hooks/use-request/basic/#刷新重复上一次请求)的效果。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里之前看源码时写的文档有误,又验证了下,refreshDeps, refreshDepsAction 是只在依赖变化时生效的。

```tsx | pure
const [userId, setUserId] = useState('1');
Expand All @@ -29,7 +29,7 @@ useEffect(() => {
}, [userId]);
```

### 刷新上一次请求
### 重复上一次请求

<code src="./demo/refreshDeps.tsx" />

Expand All @@ -41,7 +41,11 @@ useEffect(() => {

### Options

| 参数 | 说明 | 类型 | 默认值 |
| ----------------- | ------------------------------------------------------------------- | ------------ | ------ |
| refreshDeps | 依赖数组,当数组内容变化后,发起请求。同 `useEffect` 的第二个参数。 | `any[]` | `[]` |
| refreshDepsAction | 自定义依赖刷新时的请求行为,该参数会在初始化和依赖变化后被调用。 | `() => void` | - |
| 参数 | 说明 | 类型 | 默认值 |
| ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ------ |
| refreshDeps | 依赖数组。当数组内容变化后[刷新(重复上一次请求)](https://ahooks.js.org/zh-CN/hooks/use-request/basic/#刷新重复上一次请求)。同 `useEffect` 的第二个参数。 | `any[]` | `[]` |
| refreshDepsAction | 自定义依赖数组变化时的请求行为。 | `() => void` | - |

## 备注

- 如果设置 `options.manual = true`,则 `refreshDeps`, `refreshDepsAction` 都不再生效,需要通过 `run/runAsync` 手动触发请求。