Skip to content

fix(useInfiniteScroll): recheck whether to load more when the target is resized - #2964

Merged
crazylxr merged 2 commits into
alibaba:masterfrom
kelieCn:fix/use-infinite-scroll-target-resize
Sep 22, 2026
Merged

crazylxr merged 2 commits into
alibaba:masterfrom
kelieCn:fix/use-infinite-scroll-target-resize

Conversation

@kelieCn

@kelieCn kelieCn commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

🤔 这个变动的性质是?

  • 新特性提交
  • 日常 bug 修复
  • 站点、文档改进
  • 演示代码改进
  • TypeScript 定义更新
  • 包体积优化
  • 性能优化
  • 功能增强
  • 国际化改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他改动(是关于什么的改动?)

🔗 相关 Issue

Closes #2963

💡 需求背景和解决方案

配 target + isNoMore 做滚动加载的时候,触发“要不要继续加载”这个判断的只有两个地方:scroll 事件,以及每次请求成功后的那一次补检(pendingBottomScrollCheckRef,只在 direction 为 bottom 时生效)。容器或者视口的尺寸变化没人监听。

所以容器一开始比较矮、加载几页停下来之后,把容器高度调大(最大化浏览器窗口),内容就填不满容器了。这时候 scrollHeight - scrollTop <= clientHeight + threshold 其实已经成立,本来应该继续加载,但没有任何事件会去跑这个判断。同时滚动条也没了,scroll 事件永远不会再来,加载就停在那儿了。

改了三个地方:

  1. 给 target 对应的元素挂一个 ResizeObserver,尺寸变了就再跑一遍现有的 scrollMethod,组件卸载时 disconnect。target 是 document 的时候观察 document.documentElement,因为 observe() 只接受 Element。

  2. scrollMethod 用 useMemoizedFn 包了一层。observer 是在 deps 为 [] 的 effect 里创建的,回调里拿到的 scrollMethod 会一直是第一次渲染时的那个,里面 if (loading || loadingMore) return 读到的值也就一直是旧的。结果就是上一个请求还没回来的时候,尺寸一变还会再发一次请求。

  3. finalData 为空的时候直接跳过。ResizeObserver 在 observe() 的时候会先回调一次,不拦住的话,配了 manual 但还没手动触发时会自己把第一页请求掉,首次加载失败之后也会因为一次尺寸变化悄悄重试。尺寸变化只用来接着往下加载,不去发第一页。

resize-observer-polyfill 是 packages/hooks 已经有的依赖(useSize 在用),没有加新依赖。

单测加了 6 个(describe('target resize')):容器变高后会请求下一页、并且带的是最新的 data;上一个请求还没回来时尺寸变化不会重复请求;noMore 之后不请求;第一页还没加载时不请求;target 是 document 时观察的是 documentElement;卸载时会 disconnect。packages/hooks 全量 411 个测试通过。

📝 更新日志

语言 更新描述
🇺🇸 英文 Fix useInfiniteScroll not rechecking whether to load more when the target is resized.
🇨🇳 中文 修复 useInfiniteScroll 在滚动容器尺寸变化后,没有重新判断是否需要继续加载的问题。

☑️ 请求合并前的自查清单

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充
  • Changelog 已提供或无须提供

@CLAassistant

CLAassistant commented Sep 22, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@kelieCn kelieCn closed this Sep 22, 2026
@kelieCn kelieCn reopened this Sep 22, 2026
@kelieCn
kelieCn force-pushed the fix/use-infinite-scroll-target-resize branch from 882c333 to e38d344 Compare September 22, 2026 06:53

@crazylxr crazylxr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@pkg-pr-new

pkg-pr-new Bot commented Sep 22, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/ahooks@2964
npm i https://pkg.pr.new/@ahooks.js/use-url-state@2964

commit: af36488

@crazylxr
crazylxr merged commit 43003f3 into alibaba:master Sep 22, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] useInfiniteScroll 容器高度变大后不再继续加载

3 participants