import React, { useState, useEffect, useCallback } from "react"; import { Link, useLocation } from "react-router-dom"; import { Menu, X } from "lucide-react"; function Navbar() { const [open, setOpen] = useState(false); const location = useLocation(); const links = [ { name: "Home", path: "/" }, { name: "Products", path: "/products" }, { name: "Offers", path: "/offers" }, { name: "About Us", path: "/about" }, { name: "Contact", path: "/contact" }, ]; /* ✅ Stable callbacks */ const scrollToTop = useCallback(() => { requestAnimationFrame(() => { window.scrollTo({ top: 0, behavior: "smooth" }); }); }, []); const closeMenu = useCallback(() => setOpen(false), []); /* ✅ Optimized single effect: scroll + resize throttle */ useEffect(() => { // scroll on route change scrollToTop(); closeMenu(); // same-page scroll handler window.addEventListener("samePageScroll", scrollToTop); // throttle resize handler let throttleTimer; const handleResize = () => { if (!throttleTimer) { closeMenu(); throttleTimer = setTimeout(() => (throttleTimer = null), 300); } }; window.addEventListener("resize", handleResize); return () => { window.removeEventListener("samePageScroll", scrollToTop); window.removeEventListener("resize", handleResize); }; }, [scrollToTop, closeMenu, location.pathname]); return ( ); } /* ✅ Prevent unnecessary re-renders */ export default React.memo(Navbar);