Repository: alibaba/hooks
Pull request #1730 (2022-07-01 09:04:31)

fix: 修复useVirtualList - getDistanceTop调用itemHeight传参错误
<!--
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)]

### 🤔 修复useVirtualList - getDistanceTop调用itemHeight传参错误

详见：[https://github.com/alibaba/hooks/issues/1729](https://github.com/alibaba/hooks/issues/1729)

- [ ] 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

[https://github.com/alibaba/hooks/issues/1729](https://github.com/alibaba/hooks/issues/1729)

### 💡 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 |           |
| 🇨🇳 Chinese |           |

### ☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

- [ ] 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



Patch:
diff --git a/packages/hooks/src/useVirtualList/index.ts b/packages/hooks/src/useVirtualList/index.ts
index 37010d0f..74523a43 100644
--- a/packages/hooks/src/useVirtualList/index.ts
+++ b/packages/hooks/src/useVirtualList/index.ts
@@ -69,7 +69,7 @@ const useVirtualList = <T = any>(list: T[], options: Options<T>) => {
     const height = list
       .slice(0, index)
       // @ts-ignore
-      .reduce((sum, _, i) => sum + itemHeightRef.current(i, list[index]), 0);
+      .reduce((sum, _, i) => sum + itemHeightRef.current(i, list[i]), 0);
     return height;
   };
 


Test patch:
diff --git a/packages/hooks/src/useVirtualList/__tests__/index.test.ts b/packages/hooks/src/useVirtualList/__tests__/index.test.ts
index 5887b76e..02637514 100644
--- a/packages/hooks/src/useVirtualList/__tests__/index.test.ts
+++ b/packages/hooks/src/useVirtualList/__tests__/index.test.ts
@@ -78,12 +78,16 @@ describe('useVirtualList', () => {
       expect(container.scrollTop).toBe(20 * 30);
     });
 
-    it('test with dynamic height', () => {
-      setup(Array.from(Array(99999).keys()), {
+    it('test with dynamic height', async () => {
+      const list = Array.from(Array(99999).keys());
+      setup(list, {
         overscan: 0,
         containerTarget: () => container,
         wrapperTarget: () => wrapper,
-        itemHeight: (i: number) => (i % 2 === 0 ? 30 : 60),
+        itemHeight: (i: number, data) => {
+          expect(list[i] === data).toBe(true);
+          return i % 2 === 0 ? 30 : 60;
+        },
       });
 
       act(() => {