Merge pull request #38 from ragusa-it/bolt-navbar-scroll-optimization-12203924683712473060
⚡ Bolt: Optimize Navbar scroll handler
This commit was merged in pull request #38.
This commit is contained in:
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user