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: add sendable related documents #37

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/tutorial/03-learning/04-use-watcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,33 @@ const alovaInst = createAlova({
});
```

## Do not send request when state changes

Sometimes you want not to send a request when the monitored state changes. You can control whether to send a request when the monitored state changes through the sendable attribute in the Hook configuration. The sendable attribute is a function whose parameter is the `AlovaEvent` event object. Contains the array `sendArgs` composed of the parameters passed in by the `send` function, and the `method` instance of the current request, and the function returns a `truthy/falsy` value to determine whether the request needs to be triggered when the status changes (default is `true`), **throwing an error also means not triggering the request**.

```javascript
const {
// ...
// highlight-start
// When the request is not sent, the value of data is the response of the last successful request
data,
// When the request is not sent, the loading value is false
loading
// highlight-end
} = useWatcher(
() => getTodoList($currentPage),
// An array of monitored states, these state changes will trigger a request
[state],
{
sendable: methodInstance => {
// do something
// Send request only when state is 1
return state === 1;
}
}
);
```

## API

### Hook configuration
Expand All @@ -575,6 +602,7 @@ const alovaInst = createAlova({
| managedStates | Additional managed states, can be updated via updateState | Record<string | number | symbol, any> | - | - |
| debounce | Request debounce time (milliseconds), when passing in the array, you can set the debounce time separately according to the order of watchingStates | number | number[] | - | - |
| middleware | Middleware function, [Learn about alova middleware](../advanced/middleware) | (context: [AlovaFrontMiddlewareContext](../learning/use-request/#alovafrontmiddlewarecontext), next: [AlovaGuardNext](../learning/use-request/#alovaguardnext)) => Promise<any> | - | - |
| sendable | Whether to send a request when the monitored state changes | (methodInstance: AlovaEvent) => boolean | () => true | - |

### Responsive data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,32 @@ const alovaInst = createAlova({
}
});
```
## 状态改变时不发送请求

有时候你希望在监听的状态改变时不发送请求,你可以通过 Hook 配置中的 sendable 属性来控制监听的状态改变时是否发送请求,sendable 属性为一个函数,它的参数为`AlovaEvent`事件对象,包含`send`函数传入的参数所组成的数组`sendArgs`,以及当前请求的`method`实例,并且该函数返回一个`truthy/falsy`值来判断本次状态改变时是否需要触发请求(默认为`true`),**抛出错误也表示不触发请求**。

```javascript
const {
// ...
// highlight-start
// 请求未发送时,data 值为上一次请求成功的响应
data,
// 请求未发送时,loading 值为 false
loading
// highlight-end
} = useWatcher(
() => getTodoList($currentPage),
// 被监听的状态数组,这些状态变化将会触发一次请求
[state],
{
sendable: methodInstance => {
// do something
// 仅当 state 为 1 时发送请求
return state === 1;
}
}
);
```

## API

Expand All @@ -575,6 +601,7 @@ const alovaInst = createAlova({
| managedStates | 额外的监管状态,可通过 updateState 更新 | Record<string | number | symbol, any> | - | - |
| debounce | 请求防抖时间(毫秒),传入数组时可按 watchingStates 的顺序单独设置防抖时间 | number | number[] | - | - |
| middleware | 中间件函数,[了解 alova 中间件](../advanced/middleware) | (context: [AlovaFrontMiddlewareContext](../learning/use-request/#alovafrontmiddlewarecontext), next: [AlovaGuardNext](../learning/use-request/#alovaguardnext)) => Promise<any> | - | - |
| sendable | 监听的状态改变时是否发送请求 | (methodInstance: AlovaEvent) => boolean | () => true | - |

### 响应式数据

Expand Down