perf: throttle scroll event listener in Navbar
Implemented `requestAnimationFrame` throttling for the scroll event listener in `Navbar` to reduce the frequency of state updates and logic execution. - Wrapped scroll handler in `requestAnimationFrame`. - Added performance test `src/components/layout/Navbar.test.tsx` verifying logic runs once per frame instead of per event. - Verified functional correctness of scroll state updates. - Fixed `useTypingEffect` test environment.
This commit is contained in:
@@ -23,8 +23,15 @@ export function Navbar() {
|
||||
const activeIndex = navLinks.findIndex((link) => link.path === location.pathname);
|
||||
|
||||
useEffect(() => {
|
||||
let ticking = false;
|
||||
const handleScroll = () => {
|
||||
setIsScrolled(window.scrollY > 20);
|
||||
if (!ticking) {
|
||||
window.requestAnimationFrame(() => {
|
||||
setIsScrolled(window.scrollY > 20);
|
||||
ticking = false;
|
||||
});
|
||||
ticking = true;
|
||||
}
|
||||
};
|
||||
window.addEventListener('scroll', handleScroll, { passive: true });
|
||||
return () => window.removeEventListener('scroll', handleScroll);
|
||||
|
||||
Reference in New Issue
Block a user