perf(navbar): optimize scroll handler to reduce state updates

- Introduces a local tracking variable in `Navbar` scroll effect to prevent redundant `setState` calls during scroll.
- Ensures the state is correctly initialized on mount if the page is already scrolled.
- Uses `requestAnimationFrame` for efficient scroll handling.

This reduces the number of React reconciliation cycles during scrolling, improving main thread performance.

Co-authored-by: ragusa-it <196988693+ragusa-it@users.noreply.github.com>
This commit is contained in:
google-labs-jules[bot]
2026-01-30 01:44:56 +00:00
parent f3866fc2de
commit 51547318e1

View File

@@ -25,10 +25,22 @@ export function Navbar() {
useEffect(() => { useEffect(() => {
let ticking = false; let ticking = false;
// Optimization: Track last state to avoid redundant setState calls
let prevIsScrolled = window.scrollY > 20;
// Set initial state correctly if page is loaded scrolled down
if (prevIsScrolled) {
setIsScrolled(true);
}
const handleScroll = () => { const handleScroll = () => {
if (!ticking) { if (!ticking) {
window.requestAnimationFrame(() => { window.requestAnimationFrame(() => {
setIsScrolled(window.scrollY > 20); const currentIsScrolled = window.scrollY > 20;
if (currentIsScrolled !== prevIsScrolled) {
setIsScrolled(currentIsScrolled);
prevIsScrolled = currentIsScrolled;
}
ticking = false; ticking = false;
}); });
ticking = true; ticking = true;