From 43254c53939f5c39402b6cc8c8b3736576c7f053 Mon Sep 17 00:00:00 2001 From: Ayush Gade Date: Fri, 2 Oct 2026 01:06:36 +0530 Subject: [PATCH] fix: avoid scroll listener churn in useScrollPosition 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 #8142 Signed-off-by: Ayush Gade --- .../Animated-steps-list/scroll-position.js | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/components/Animated-steps-list/scroll-position.js b/src/components/Animated-steps-list/scroll-position.js index c3faed278c002f..0b22e5caaa42a0 100644 --- a/src/components/Animated-steps-list/scroll-position.js +++ b/src/components/Animated-steps-list/scroll-position.js @@ -2,25 +2,24 @@ import { useEffect, useState } from "react"; const useScrollPosition = () => { const [scrollPosition, setScrollPosition] = useState(0); - const [ticking, setTicking] = useState(false); useEffect(() => { - window.addEventListener("scroll", handleScroll); - - return () => { - window.removeEventListener("scroll", handleScroll); + // Local, not state: this throttle flag must not trigger re-renders or + // re-run the effect (which would re-attach the listener every frame). + let ticking = false; + const handleScroll = () => { + if (!ticking) { + ticking = true; + window.requestAnimationFrame(() => { + setScrollPosition(window.scrollY); + ticking = false; + }); + } }; - }, [ticking]); + window.addEventListener("scroll", handleScroll, { passive: true }); + return () => window.removeEventListener("scroll", handleScroll); + }, []); - function handleScroll() { - if (!ticking) { - window.requestAnimationFrame(function () { - setScrollPosition(window.scrollY); - setTicking(false); - }); - setTicking(true); - } - } return scrollPosition; };