fix(useInfiniteScroll): recheck whether to load more when the target is resized - #2964
Merged
crazylxr merged 2 commits intoSep 22, 2026
Merged
Conversation
kelieCn
force-pushed
the
fix/use-infinite-scroll-target-resize
branch
from
September 22, 2026 06:53
882c333 to
e38d344
Compare
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤔 这个变动的性质是?
🔗 相关 Issue
Closes #2963
💡 需求背景和解决方案
配
target+isNoMore做滚动加载的时候,触发“要不要继续加载”这个判断的只有两个地方:scroll 事件,以及每次请求成功后的那一次补检(pendingBottomScrollCheckRef,只在direction为bottom时生效)。容器或者视口的尺寸变化没人监听。所以容器一开始比较矮、加载几页停下来之后,把容器高度调大(最大化浏览器窗口),内容就填不满容器了。这时候
scrollHeight - scrollTop <= clientHeight + threshold其实已经成立,本来应该继续加载,但没有任何事件会去跑这个判断。同时滚动条也没了,scroll 事件永远不会再来,加载就停在那儿了。改了三个地方:
给
target对应的元素挂一个ResizeObserver,尺寸变了就再跑一遍现有的scrollMethod,组件卸载时disconnect。target是document的时候观察document.documentElement,因为observe()只接受Element。scrollMethod用useMemoizedFn包了一层。observer 是在deps为[]的 effect 里创建的,回调里拿到的scrollMethod会一直是第一次渲染时的那个,里面if (loading || loadingMore) return读到的值也就一直是旧的。结果就是上一个请求还没回来的时候,尺寸一变还会再发一次请求。finalData为空的时候直接跳过。ResizeObserver在observe()的时候会先回调一次,不拦住的话,配了manual但还没手动触发时会自己把第一页请求掉,首次加载失败之后也会因为一次尺寸变化悄悄重试。尺寸变化只用来接着往下加载,不去发第一页。resize-observer-polyfill是packages/hooks已经有的依赖(useSize在用),没有加新依赖。单测加了 6 个(
describe('target resize')):容器变高后会请求下一页、并且带的是最新的data;上一个请求还没回来时尺寸变化不会重复请求;noMore之后不请求;第一页还没加载时不请求;target是document时观察的是documentElement;卸载时会disconnect。packages/hooks全量 411 个测试通过。📝 更新日志
useInfiniteScrollnot rechecking whether to load more when the target is resized.useInfiniteScroll在滚动容器尺寸变化后,没有重新判断是否需要继续加载的问题。☑️ 请求合并前的自查清单