Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/packages/infiniteloading/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import { InfiniteLoading } from '@nutui/nutui-react'
| onRefresh | 下拉刷新事件回调 | `() => Promise<void>` | `-` |
| onLoadMore | 继续加载的回调函数 | `() => Promise<void>` | `-` |
| onScroll | 实时监听滚动高度 | `(param: number) => void` | `-` |
| defaultScrollTop | 默认滚动距离 | `number` | `-` |

## 主题定制

Expand Down
12 changes: 12 additions & 0 deletions src/packages/infiniteloading/infiniteloading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
onRefresh: () => Promise<void>
onLoadMore: () => Promise<void>
onScroll: (param: number) => void
defaultScrollTop?: number
}

declare let window: Window & { webkitRequestAnimationFrame: any } & {
Expand Down Expand Up @@ -60,6 +61,7 @@
onRefresh,
onLoadMore,
onScroll,
defaultScrollTop,
...restProps
} = {
...defaultProps,
Expand All @@ -77,6 +79,16 @@

const classes = classNames(classPrefix, className, `${classPrefix}-${type}`)

useEffect(() => {
if (defaultScrollTop) {
const childHeight = (getRefreshTop().firstElementChild as HTMLElement).offsetHeight || 0;

Check failure on line 84 in src/packages/infiniteloading/infiniteloading.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `·(getRefreshTop().firstElementChild·as·HTMLElement).offsetHeight·||·0;` with `⏎········(getRefreshTop().firstElementChild·as·HTMLElement).offsetHeight·||·0`
refreshMaxH.current = Math.floor(childHeight * 1 + 10);

Check failure on line 85 in src/packages/infiniteloading/infiniteloading.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `;`
setTimeout(() =>{

Check failure on line 86 in src/packages/infiniteloading/infiniteloading.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `·`
scrollEl.current.scrollTop = defaultScrollTop

Check failure on line 87 in src/packages/infiniteloading/infiniteloading.tsx

View workflow job for this annotation

GitHub Actions / build

'scrollEl.current' is possibly 'null'.

Check failure on line 87 in src/packages/infiniteloading/infiniteloading.tsx

View workflow job for this annotation

GitHub Actions / build

Property 'scrollTop' does not exist on type 'HTMLElement | Window'. Did you mean 'scrollTo'?
}, 10)
}

Check warning on line 89 in src/packages/infiniteloading/infiniteloading.tsx

View check run for this annotation

Codecov / codecov/patch

src/packages/infiniteloading/infiniteloading.tsx#L84-L89

Added lines #L84 - L89 were not covered by tests
}, [defaultScrollTop]);

Check failure on line 90 in src/packages/infiniteloading/infiniteloading.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `;`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

建议优化 useEffect 实现

当前实现存在以下需要改进的地方:

  1. 缺少清理函数以处理组件卸载情况
  2. setTimeout 使用了魔法数字 (10ms)
  3. 没有处理可能的空值情况
  4. 可能与 DOM 更新产生竞态条件

建议按照以下方式重构:

 useEffect(() => {
   if (defaultScrollTop) {
+    const element = getRefreshTop();
+    if (!element?.firstElementChild) return;
+
     const childHeight = (getRefreshTop().firstElementChild as HTMLElement).offsetHeight || 0;
     refreshMaxH.current = Math.floor(childHeight * 1 + 10);
-    setTimeout(() =>{
-      scrollEl.current.scrollTop = defaultScrollTop
-    }, 10)
+    const timeoutId = setTimeout(() => {
+      if (scrollEl.current) {
+        scrollEl.current.scrollTop = defaultScrollTop;
+      }
+    }, 100); // 使用更合理的延迟时间
+
+    return () => clearTimeout(timeoutId);
   }
 }, [defaultScrollTop]);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
useEffect(() => {
if (defaultScrollTop) {
const childHeight = (getRefreshTop().firstElementChild as HTMLElement).offsetHeight || 0;
refreshMaxH.current = Math.floor(childHeight * 1 + 10);
setTimeout(() =>{
scrollEl.current.scrollTop = defaultScrollTop
}, 10)
}
}, [defaultScrollTop]);
useEffect(() => {
if (defaultScrollTop) {
+ const element = getRefreshTop();
+ if (!element?.firstElementChild) return;
+
const childHeight = (getRefreshTop().firstElementChild as HTMLElement).offsetHeight || 0;
refreshMaxH.current = Math.floor(childHeight * 1 + 10);
- setTimeout(() =>{
- scrollEl.current.scrollTop = defaultScrollTop
- }, 10)
+ const timeoutId = setTimeout(() => {
+ if (scrollEl.current) {
+ scrollEl.current.scrollTop = defaultScrollTop;
+ }
+ }, 100); // 使用更合理的延迟时间
+
+ return () => clearTimeout(timeoutId);
}
}, [defaultScrollTop]);
🧰 Tools
🪛 ESLint

[error] 84-84: Replace ·(getRefreshTop().firstElementChild·as·HTMLElement).offsetHeight·||·0; with ⏎········(getRefreshTop().firstElementChild·as·HTMLElement).offsetHeight·||·0

(prettier/prettier)


[error] 85-85: Delete ;

(prettier/prettier)


[error] 86-86: Insert ·

(prettier/prettier)


[error] 90-90: Delete ;

(prettier/prettier)


useEffect(() => {
if (target && document.getElementById(target)) {
scrollEl.current = document.getElementById(target)
Expand Down
Loading