Repository navigation
[Learn] Fix useScrollPosition re-attaching its scroll listener on every frame - #8164
Conversation
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review. 📝 WalkthroughWalkthrough
ChangesScroll Position Throttle
Priority: ⬇️ Low Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix · Severity of issue fixed: Low Suggested reviewers: Merge Risk: ⚪ Minimal · up to The hook avoids repeatedly reattaching its scroll listener while preserving animation updates. No actionable merge-blocking risk remains after normal checks. Architecture SummaryArchitecture risk: 🔵 Low · up to The change affects 1 system. Changed systems: Architecture concerns Review detailsSystems and components
Before / after behavior
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Preview deployment for PR #8164 removed. This PR preview was automatically pruned because we keep only the 3 most recently updated previews on GitHub Pages to stay within deployment size limits. If needed, push a new commit to this PR to generate a fresh preview. |
tejaswiverma121-byte
left a comment
There was a problem hiding this comment.
Minor Improvements
If this hook is ever used in a server-rendered environment, guard window access before registering the listener.A brief inline comment explaining why the throttle state is intentionally local to the effect could help future maintainers avoid reintroducing the same bug.
Optional test: a small unit/integration test around the hook would make this regression harder to reintroduce later.
Move the rAF throttle flag out of React state into a local variable scoped inside the effect. Storing it in state re-ran the effect every animation frame, detaching and re-attaching the scroll listener ~60x/sec. The flag is internal bookkeeping the UI never renders, so a local let suffices; the listener now attaches once and is passive. Behavior is unchanged. Fixes layer5io#8142 Signed-off-by: Ayush Gade <ayushgade23@gmail.com>
ed12bf6 to
43254c5
Compare
PARTH-TUSSLE
left a comment
There was a problem hiding this comment.
LGTM 👍 a proper hook test would need extra tooling (Testing Library + jsdom), so I'd suggest handling that in a separate follow-up issue if anything. So nothing blocking from my side.
|
Thanks @ayushgade06 💯 |
Description
This PR fixes #8142.
useScrollPosition(src/components/Animated-steps-list/scroll-position.js) powers the scroll-scrubbed terminal animation on the Meshery Getting Started page. It throttles updates usingrequestAnimationFrame, but thetickingthrottle flag was stored in React state and included in the effect dependency array ([ticking]).Since
tickingchanges fromfalse → true → falseon every animation frame, the effect was continuously tearing down and recreating the scroll listener while scrolling.Performance impact (before this fix)
tickingchange re-ran the effect, which removed and immediately re-added the scroll listener.setTicking(true)andsetTicking(false)on each frame, even thoughtickingis not used for rendering.Fix
tickingstate with a local variable scoped inside the effect. The flag now updates synchronously without causing re-renders.[], so the scroll listener is attached once on mount and removed once on unmount.{ passive: true }to the scroll listener so the browser does not need to wait for the handler before continuing with scrolling.scrollPositionremains the only state that triggers a render, so the animation behavior stays the same.Net result
The visible behavior remains unchanged, but the repeated listener setup and throttle-related re-renders are eliminated. The hook now keeps one listener active instead of repeatedly attaching and removing it during scrolling.
Notes for Reviewers
/cloud-native-management/meshery/getting-started). The animation scrubs as expected.scrollPosition.Signed commits