From d7fa79cb9a1b933dd34f26852dc0d8fda9504333 Mon Sep 17 00:00:00 2001 From: huangcheng Date: Thu, 3 Aug 2023 09:26:26 +0800 Subject: [PATCH] feat(useInfiniteScroll): add error result (#2275) * feat(useInfiniteScroll): add error result * test: rename --- .../hooks/src/useDocumentVisibility/index.zh-CN.md | 4 ++-- packages/hooks/src/useDrop/index.en-US.md | 8 ++++---- packages/hooks/src/useDrop/index.zh-CN.md | 8 ++++---- .../src/useInfiniteScroll/__tests__/index.test.ts | 11 +++++++++++ packages/hooks/src/useInfiniteScroll/index.en-US.md | 5 +++-- packages/hooks/src/useInfiniteScroll/index.tsx | 3 ++- packages/hooks/src/useInfiniteScroll/index.zh-CN.md | 5 +++-- 7 files changed, 29 insertions(+), 15 deletions(-) diff --git a/packages/hooks/src/useDocumentVisibility/index.zh-CN.md b/packages/hooks/src/useDocumentVisibility/index.zh-CN.md index e38ef7be72..349930a52e 100644 --- a/packages/hooks/src/useDocumentVisibility/index.zh-CN.md +++ b/packages/hooks/src/useDocumentVisibility/index.zh-CN.md @@ -21,6 +21,6 @@ const documentVisibility = useDocumentVisibility(); ### Result -| 参数 | 说明 | 类型 | -| ------------------ | ------------------------------------ | -------------------------------------------------- | +| 参数 | 说明 | 类型 | +| ------------------ | ------------------------------ | -------------------------------------------------- | | documentVisibility | 判断 document 是否处于可见状态 | `visible`\| `hidden` \| `prerender` \| `undefined` | diff --git a/packages/hooks/src/useDrop/index.en-US.md b/packages/hooks/src/useDrop/index.en-US.md index 15e05267a0..a6755c292c 100644 --- a/packages/hooks/src/useDrop/index.en-US.md +++ b/packages/hooks/src/useDrop/index.en-US.md @@ -53,11 +53,11 @@ useDrag( #### DragImageOptions -| 参数 | 说明 | 类型 | 默认值 | -| ------- | ----------------------------------------------------------- | ------------------- | ------ | +| 参数 | 说明 | 类型 | 默认值 | +| ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------- | ------ | | image | An image Element element to use for the drag feedback image. The image will typically be an [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img) element but it can also be a [``](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 diff --git a/packages/hooks/src/useDrop/index.zh-CN.md b/packages/hooks/src/useDrop/index.zh-CN.md index 1a007169c5..c1dc96d4ae 100644 --- a/packages/hooks/src/useDrop/index.zh-CN.md +++ b/packages/hooks/src/useDrop/index.zh-CN.md @@ -53,11 +53,11 @@ useDrag( #### DragImageOptions -| 参数 | 说明 | 类型 | 默认值 | -| ------- | ---------------------------- | ------------------- | ------ | +| 参数 | 说明 | 类型 | 默认值 | +| ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------- | ------ | | image | 拖拽过程中跟随鼠标指针的图像。图像通常是一个 [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img) 元素,但也可以是 [``](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 diff --git a/packages/hooks/src/useInfiniteScroll/__tests__/index.test.ts b/packages/hooks/src/useInfiniteScroll/__tests__/index.test.ts index c6cfd6cdfd..12d0c8654b 100644 --- a/packages/hooks/src/useInfiniteScroll/__tests__/index.test.ts +++ b/packages/hooks/src/useInfiniteScroll/__tests__/index.test.ts @@ -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'); + }); }); diff --git a/packages/hooks/src/useInfiniteScroll/index.en-US.md b/packages/hooks/src/useInfiniteScroll/index.en-US.md index 47dc398ac8..ae685488b4 100644 --- a/packages/hooks/src/useInfiniteScroll/index.en-US.md +++ b/packages/hooks/src/useInfiniteScroll/index.en-US.md @@ -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` | | reload | Load the first page of data, it will automatically catch the exception, and handle it through `options.onError` | `() => void` | diff --git a/packages/hooks/src/useInfiniteScroll/index.tsx b/packages/hooks/src/useInfiniteScroll/index.tsx index 27eac28293..a559c562d1 100644 --- a/packages/hooks/src/useInfiniteScroll/index.tsx +++ b/packages/hooks/src/useInfiniteScroll/index.tsx @@ -31,7 +31,7 @@ const useInfiniteScroll = ( 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) { @@ -122,6 +122,7 @@ const useInfiniteScroll = ( return { data: finalData, loading: !loadingMore && loading, + error, loadingMore, noMore, diff --git a/packages/hooks/src/useInfiniteScroll/index.zh-CN.md b/packages/hooks/src/useInfiniteScroll/index.zh-CN.md index 8797c9bd44..314cf7af4c 100644 --- a/packages/hooks/src/useInfiniteScroll/index.zh-CN.md +++ b/packages/hooks/src/useInfiniteScroll/index.zh-CN.md @@ -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` | | reload | 加载第一页数据,会自动捕获异常,通过 `options.onError` 处理 | `() => void` |