diff --git a/packages/hooks/src/useInfiniteScroll/__tests__/index.test.ts b/packages/hooks/src/useInfiniteScroll/__tests__/index.test.ts index 3ef3d60157..e965a7abac 100644 --- a/packages/hooks/src/useInfiniteScroll/__tests__/index.test.ts +++ b/packages/hooks/src/useInfiniteScroll/__tests__/index.test.ts @@ -323,4 +323,15 @@ describe('useInfiniteScroll', () => { expect(result.current.loading).toBeFalsy(); }); + + it('error state', 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 9d6d27e954..5c432536a8 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) { @@ -120,6 +120,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` |