fix(useInfiniteScroll): Closure issue preventing access to the latest data during the initial loadMore() call#2896
fix(useInfiniteScroll): Closure issue preventing access to the latest data during the initial loadMore() call#2896bowencool wants to merge 5 commits intoalibaba:masterfrom
Conversation
commit: |
|
scrollMethod() 从 onSuccess 里挪到 useUpdateEffect(() => scrollMethod(), [finalData]) 后,触发范围变宽了:现在只要 finalData 发生变化,就会执行 scrollMethod(),不再局限于“请求成功后的自动补拉”这个场景。这样一来,外部调用 mutate 更新数据时,也可能意外触发 loadMore(),这和原来的行为不完全一致,存在潜在回归风险。 我建议把触发条件再收窄一点,例如只在“当前这次请求成功后、且需要做 bottom 场景补判”时,再通过一个 ref 或 flag 在 effect 里触发 scrollMethod(),避免所有 finalData 变更都进入这条路径。 另外,这个 PR 目前也缺少对应的回归测试,至少建议补两类用例: 首次加载后因为容器未撑满而自动触发 loadMore(),并且第二次请求能够拿到最新的 finalData |
There was a problem hiding this comment.
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 requestonSuccesscallback. - Add a
useUpdateEffectthat triggersscrollMethod()whenfinalDatachanges.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…ch only, add regression tests
There was a problem hiding this comment.
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.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
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.
已经按照建议修改,已经解决所有的评论。 |
[中文版模板 / Chinese template]
🤔 This is a ...
🔗 Related issue link
💡 Background and solution
Before:
onSuccess() -> setFinalData() and scrollMethod() -> loadMore() -> finalData.After:
onSuccess() -> setFinalData(),useEffect() -> scrollMethod() -> loadMore() -> finalDataThe
scrollMethod() -> loadMore() -> finalDatais within the closure of the old flow, causingloadMore()to access the old state.📝 Changelog
loadMore())loadMore()时无法访问最新数据)☑️ Self Check before Merge