Skip to content

Commit

Permalink
feat(useInfiniteScroll): add error result (#2275)
Browse files Browse the repository at this point in the history
* feat(useInfiniteScroll): add error result

* test: rename
  • Loading branch information
hchlq authored Aug 3, 2023
1 parent 5b056d0 commit d7fa79c
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/hooks/src/useDocumentVisibility/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ const documentVisibility = useDocumentVisibility();

### Result

| 参数 | 说明 | 类型 |
| ------------------ | ------------------------------------ | -------------------------------------------------- |
| 参数 | 说明 | 类型 |
| ------------------ | ------------------------------ | -------------------------------------------------- |
| documentVisibility | 判断 document 是否处于可见状态 | `visible`\| `hidden` \| `prerender` \| `undefined` |
8 changes: 4 additions & 4 deletions packages/hooks/src/useDrop/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ useDrag<T>(

#### DragImageOptions

| 参数 | 说明 | 类型 | 默认值 |
| ------- | ----------------------------------------------------------- | ------------------- | ------ |
| 参数 | 说明 | 类型 | 默认值 |
| ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------- | ------ |
| image | An image Element element to use for the drag feedback image. The image will typically be an [`<img>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img) element but it can also be a [`<canvas>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas) or any other visible element | `string \| Element` | - |
| offsetX | the horizontal offset within the image | `number` | 0 |
| offsetY | the vertical offset within the image | `number` | 0 |
| offsetX | the horizontal offset within the image | `number` | 0 |
| offsetY | the vertical offset within the image | `number` | 0 |

### useDrop

Expand Down
8 changes: 4 additions & 4 deletions packages/hooks/src/useDrop/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ useDrag<T>(

#### DragImageOptions

| 参数 | 说明 | 类型 | 默认值 |
| ------- | ---------------------------- | ------------------- | ------ |
| 参数 | 说明 | 类型 | 默认值 |
| ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------- | ------ |
| image | 拖拽过程中跟随鼠标指针的图像。图像通常是一个 [`<img>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img) 元素,但也可以是 [`<canvas>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas) 或任何其他图像元素。 | `string \| Element` | - |
| offsetX | 水平偏移 | `number` | 0 |
| offsetY | 垂直偏移 | `number` | 0 |
| offsetX | 水平偏移 | `number` | 0 |
| offsetY | 垂直偏移 | `number` | 0 |

### useDrop

Expand Down
11 changes: 11 additions & 0 deletions packages/hooks/src/useInfiniteScroll/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,15 @@ describe('useInfiniteScroll', () => {
loadMore();
});
});

it('error result', async () => {
const { result } = setup(async () => {
throw new Error('error message');
});
await act(async () => {
jest.advanceTimersByTime(1000);
});

expect(result.current.error?.message).toBe('error message');
});
});
5 changes: 3 additions & 2 deletions packages/hooks/src/useInfiniteScroll/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ const {
### Result

| Property | Description | Type |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------ |
| data | The data returned by the service, where the `list` attribute is the aggregated data | `TData` \| `undefined` |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------ | ----------- |
| data | The data returned by the service, where the `list` attribute is the aggregated data | `TData` \ | `undefined` |
| loading | Is the first request in progress | `boolean` |
| loadingMore | Is more data request in progress | `boolean` |
| noMore | Whether there is no more data, it will take effect after configuring `options.isNoMore` | `boolean` |
| error | Request error message | `Error` |
| loadMore | Load more data, it will automatically catch the exception, and handle it through `options.onError` | `() => void` |
| loadMoreAsync | Load more data, which is consistent with the behavior of `loadMore`, but returns Promise, so you need to handle the exception yourself | `() => Promise<TData>` |
| reload | Load the first page of data, it will automatically catch the exception, and handle it through `options.onError` | `() => void` |
Expand Down
3 changes: 2 additions & 1 deletion packages/hooks/src/useInfiniteScroll/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const useInfiniteScroll = <TData extends Data>(
return isNoMore(finalData);
}, [finalData]);

const { loading, run, runAsync, cancel } = useRequest(
const { loading, error, run, runAsync, cancel } = useRequest(
async (lastData?: TData) => {
const currentData = await service(lastData);
if (!lastData) {
Expand Down Expand Up @@ -122,6 +122,7 @@ const useInfiniteScroll = <TData extends Data>(
return {
data: finalData,
loading: !loadingMore && loading,
error,
loadingMore,
noMore,

Expand Down
5 changes: 3 additions & 2 deletions packages/hooks/src/useInfiniteScroll/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ const {
### Result

| 参数 | 说明 | 类型 |
| ------------- | -------------------------------------------------------------------------- | ------------------------ |
| data | service 返回的数据,其中的 `list` 属性为聚合后数据 | `TData` \| `undefined` |
| ------------- | -------------------------------------------------------------------------- | ------------------------ | ----------- |
| data | service 返回的数据,其中的 `list` 属性为聚合后数据 | `TData` \ | `undefined` |
| loading | 是否正在进行首次请求 | `boolean` |
| loadingMore | 是否正在进行更多数据请求 | `boolean` |
| noMore | 是否没有更多数据了,配置 `options.isNoMore` 后生效 | `boolean` |
| error | 请求错误消息 | `Error` |
| loadMore | 加载更多数据,会自动捕获异常,通过 `options.onError` 处理 | `() => void` |
| loadMoreAsync | 加载更多数据,与 `loadMore` 行为一致,但返回的是 Promise,需要自行处理异常 | `() => Promise<TData>` |
| reload | 加载第一页数据,会自动捕获异常,通过 `options.onError` 处理 | `() => void` |
Expand Down

0 comments on commit d7fa79c

Please sign in to comment.