Skip to content

fix(useInfiniteScroll): Closure issue preventing access to the latest data during the initial loadMore() call#2896

Open
bowencool wants to merge 5 commits intoalibaba:masterfrom
bowencool:master
Open

fix(useInfiniteScroll): Closure issue preventing access to the latest data during the initial loadMore() call#2896
bowencool wants to merge 5 commits intoalibaba:masterfrom
bowencool:master

Conversation

@bowencool
Copy link
Copy Markdown
Contributor

[中文版模板 / Chinese template]

🤔 This is a ...

  • New feature
  • 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

💡 Background and solution

Before: onSuccess() -> setFinalData() and scrollMethod() -> loadMore() -> finalData.
After: onSuccess() -> setFinalData(), useEffect() -> scrollMethod() -> loadMore() -> finalData

The scrollMethod() -> loadMore() -> finalData is within the closure of the old flow, causing loadMore() to access the old state.

📝 Changelog

Language Changelog
🇺🇸 English Fix closure issue (cannot access the latest data during the first loadMore())
🇨🇳 Chinese 修复闭包问题(第一次 loadMore() 时无法访问最新数据)

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

@bowencool bowencool marked this pull request as draft January 23, 2026 08:38
@bowencool bowencool marked this pull request as ready for review January 23, 2026 08:39
@bowencool bowencool changed the title fix closure issue fix: closure issue Jan 23, 2026
@bowencool bowencool changed the title fix: closure issue fix(useInfiniteScroll): Closure issue preventing access to the latest data during the initial loadMore() call Jan 23, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Jan 23, 2026

npm i https://pkg.pr.new/ahooks@2896
npm i https://pkg.pr.new/@ahooks.js/use-url-state@2896

commit: 2fc7b06

@crazylxr
Copy link
Copy Markdown
Collaborator

scrollMethod() 从 onSuccess 里挪到 useUpdateEffect(() => scrollMethod(), [finalData]) 后,触发范围变宽了:现在只要 finalData 发生变化,就会执行 scrollMethod(),不再局限于“请求成功后的自动补拉”这个场景。这样一来,外部调用 mutate 更新数据时,也可能意外触发 loadMore(),这和原来的行为不完全一致,存在潜在回归风险。

我建议把触发条件再收窄一点,例如只在“当前这次请求成功后、且需要做 bottom 场景补判”时,再通过一个 ref 或 flag 在 effect 里触发 scrollMethod(),避免所有 finalData 变更都进入这条路径。

另外,这个 PR 目前也缺少对应的回归测试,至少建议补两类用例:

首次加载后因为容器未撑满而自动触发 loadMore(),并且第二次请求能够拿到最新的 finalData
调用 mutate 更新数据时,不会额外触发 loadMore()

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adjusts useInfiniteScroll to avoid a stale-closure problem where the first auto loadMore() could read outdated finalData, by deferring the scroll check until after finalData has updated.

Changes:

  • Remove the post-success scrollMethod() call from the request onSuccess callback.
  • Add a useUpdateEffect that triggers scrollMethod() when finalData changes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

bowencool and others added 3 commits March 31, 2026 01:00
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@bowencool
Copy link
Copy Markdown
Contributor Author

scrollMethod() 从 onSuccess 里挪到 useUpdateEffect(() => scrollMethod(), [finalData]) 后,触发范围变宽了:现在只要 finalData 发生变化,就会执行 scrollMethod(),不再局限于“请求成功后的自动补拉”这个场景。这样一来,外部调用 mutate 更新数据时,也可能意外触发 loadMore(),这和原来的行为不完全一致,存在潜在回归风险。

我建议把触发条件再收窄一点,例如只在“当前这次请求成功后、且需要做 bottom 场景补判”时,再通过一个 ref 或 flag 在 effect 里触发 scrollMethod(),避免所有 finalData 变更都进入这条路径。

另外,这个 PR 目前也缺少对应的回归测试,至少建议补两类用例:

首次加载后因为容器未撑满而自动触发 loadMore(),并且第二次请求能够拿到最新的 finalData 调用 mutate 更新数据时,不会额外触发 loadMore()

已经按照建议修改,已经解决所有的评论。

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.

3 participants