Repository: alibaba/hooks
Pull request #1790 (2022-08-01 07:32:33)

fix(createUpdateEffect): Using on-demand loading will report an error.
<!--
First of all, thank you for your contribution! 😄

New feature please send a pull request to master branch.
Pull requests will be merged after one of the collaborators approve.
Please makes sure that these forms are filled before submitting your pull request, thank you!
-->

[[中文版模板 / Chinese template](https://github.com/alibaba/hooks/blob/master/.github/PULL_REQUEST_TEMPLATE/pr_cn.md)]

### 🤔 This is a ...

- [ ] New feature
- [x] Bug fix
- [ ] Site / documentation update
- [ ] Demo update
- [ ] TypeScript definition update
- [ ] Bundle size optimization
- [ ] Performance optimization
- [ ] Enhancement feature
- [ ] Internationalization
- [ ] Refactoring
- [ ] Code style optimization
- [ ] Test Case
- [ ] Branch merge
- [ ] Other (about what?)

### 🔗 Related issue link

close: #1791 

### 💡 Background and solution

<!--
1. Describe the problem and the scenario.
2. GIF or snapshot should be provided if includes UI/interactive modification.
3. How to fix the problem, and list final API implementation and usage sample if that is a new feature.
-->

### 📝 Changelog

<!--
Describe changes from the user side, and list all potential break changes or other risks.
--->

| Language   | Changelog |
| ---------- | --------- |
| 🇺🇸 English | Fix createUpdateEffect will report an error when loading on demand. |
| 🇨🇳 Chinese | 修复按需加载时createUpdateEffect会报错。 |

### ☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

- [x] Doc is updated/provided or not needed
- [x] Demo is updated/provided or not needed
- [ ] TypeScript definition is updated/provided or not needed
- [ ] Changelog is provided or not needed


CLAassistant: [![CLA assistant check](https://cla-assistant.io/pull/badge/not_signed)](https://cla-assistant.io/alibaba/hooks?pullRequest=1790) <br/>Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our [Contributor License Agreement](https://cla-assistant.io/alibaba/hooks?pullRequest=1790) before we can accept your contribution.<br/><sub>You have signed the CLA already but the status is still pending? Let us [recheck](https://cla-assistant.io/check/alibaba/hooks?pullRequest=1790) it.</sub>
mushan0x0: @miracles1919 看看呢

Patch:
diff --git a/packages/hooks/src/createUpdateEffect/index.ts b/packages/hooks/src/createUpdateEffect/index.ts
index 12123bb5..6944bf09 100644
--- a/packages/hooks/src/createUpdateEffect/index.ts
+++ b/packages/hooks/src/createUpdateEffect/index.ts
@@ -22,3 +22,5 @@ export const createUpdateEffect: (hook: EffectHookType) => EffectHookType =
       }
     }, deps);
   };
+
+export default createUpdateEffect;
diff --git a/packages/hooks/src/usePagination/index.en-US.md b/packages/hooks/src/usePagination/index.en-US.md
index 714a17c6..834b8a1f 100644
--- a/packages/hooks/src/usePagination/index.en-US.md
+++ b/packages/hooks/src/usePagination/index.en-US.md
@@ -79,4 +79,5 @@ const {
 | Property        | Description                                                                                                                                      | Type                   | Default |
 | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------- | ------- |
 | defaultPageSize | Default page size                                                                                                                                | `number`               | -       |
+| defaultCurrent  | Number of pages on initial request                                                                                                               | `number`               | -       |
 | refreshDeps     | Changes in `refreshDeps` will reset current to the first page and re-initiate the request. Generally, you can put the dependent conditions here. | `React.DependencyList` | `[]`    |
diff --git a/packages/hooks/src/usePagination/index.ts b/packages/hooks/src/usePagination/index.ts
index 45661747..2e821931 100644
--- a/packages/hooks/src/usePagination/index.ts
+++ b/packages/hooks/src/usePagination/index.ts
@@ -8,10 +8,10 @@ const usePagination = <TData extends Data, TParams extends Params>(
   service: Service<TData, TParams>,
   options: PaginationOptions<TData, TParams> = {},
 ) => {
-  const { defaultPageSize = 10, ...rest } = options;
+  const { defaultPageSize = 10, defaultCurrent = 1, ...rest } = options;
 
   const result = useRequest(service, {
-    defaultParams: [{ current: 1, pageSize: defaultPageSize }],
+    defaultParams: [{ current: defaultCurrent, pageSize: defaultPageSize }],
     refreshDepsAction: () => {
       // eslint-disable-next-line @typescript-eslint/no-use-before-define
       changeCurrent(1);
diff --git a/packages/hooks/src/usePagination/index.zh-CN.md b/packages/hooks/src/usePagination/index.zh-CN.md
index ce0d605e..4c0d3387 100644
--- a/packages/hooks/src/usePagination/index.zh-CN.md
+++ b/packages/hooks/src/usePagination/index.zh-CN.md
@@ -79,4 +79,5 @@ const {
 | 参数            | 说明                                                                                        | 类型                   | 默认值 |
 | --------------- | ------------------------------------------------------------------------------------------- | ---------------------- | ------ |
 | defaultPageSize | 默认分页数量                                                                                | `number`               | -      |
+| defaultCurrent  | 初次请求时的页数                                                                            | `number`               | -      |
 | refreshDeps     | `refreshDeps` 变化，会重置 current 到第一页，并重新发起请求，一般你可以把依赖的条件放这里。 | `React.DependencyList` | `[]`   |
diff --git a/packages/hooks/src/usePagination/types.ts b/packages/hooks/src/usePagination/types.ts
index 878fd3e8..7beabc75 100644
--- a/packages/hooks/src/usePagination/types.ts
+++ b/packages/hooks/src/usePagination/types.ts
@@ -24,4 +24,5 @@ export interface PaginationResult<TData extends Data, TParams extends Params>
 export interface PaginationOptions<TData extends Data, TParams extends Params>
   extends Options<TData, TParams> {
   defaultPageSize?: number;
+  defaultCurrent?: number;
 }


Test patch:
diff --git a/packages/hooks/src/usePagination/__tests__/index.test.ts b/packages/hooks/src/usePagination/__tests__/index.test.ts
index 2b1e415b..66c86fd7 100644
--- a/packages/hooks/src/usePagination/__tests__/index.test.ts
+++ b/packages/hooks/src/usePagination/__tests__/index.test.ts
@@ -128,4 +128,35 @@ describe('usePagination', () => {
     expect(hook.result.current.pagination.current).toEqual(1);
     expect(hook.result.current.pagination.pageSize).toEqual(20);
   });
+
+  it('should default params work', async () => {
+    queryArgs = undefined;
+    act(() => {
+      hook = setUp(asyncFn, {
+        defaultPageSize: 5,
+        defaultCurrent: 2,
+      });
+    });
+    expect(hook.result.current.loading).toEqual(true);
+    expect(queryArgs.current).toEqual(2);
+    expect(queryArgs.pageSize).toEqual(5);
+    await hook.waitForNextUpdate();
+
+    expect(hook.result.current.loading).toEqual(false);
+
+    expect(hook.result.current.pagination.current).toEqual(2);
+    expect(hook.result.current.pagination.pageSize).toEqual(5);
+    expect(hook.result.current.pagination.total).toEqual(55);
+    expect(hook.result.current.pagination.totalPage).toEqual(11);
+
+    act(() => {
+      hook.result.current.pagination.changeCurrent(3);
+    });
+    expect(hook.result.current.loading).toEqual(true);
+    expect(queryArgs.current).toEqual(3);
+    expect(queryArgs.pageSize).toEqual(5);
+    await hook.waitForNextUpdate();
+    expect(hook.result.current.pagination.current).toEqual(3);
+    expect(hook.result.current.pagination.pageSize).toEqual(5);
+  });
 });