My scrollHandler function works just fine, when I’m on PC or when I open responsive inspect, everything works as it should, but when I open the application on a mobile phone, MENU always has the sticky property.
App link: https://fashion-store-23mp.netlify.app/
import React, { useContext, useEffect, useState } from "react";
import { SidebarContext } from "../contexts/SidebarContext";
import { CartContext } from "../contexts/CartContext";
import { BsBag } from "react-icons/bs";
import { Link } from "react-router-dom";
import Logo from "../img/logo.svg";
const Header = () => {
// header state
const [isActive, setIsActive] = useState(false);
const [currentScroll, setCurrentScroll] = useState(window.currentScroll);
const { isOpen, setIsOpen } = useContext(SidebarContext);
const { itemAmount } = useContext(CartContext);
// event listener
useEffect(() => {
let prevScroll = window.scrollY;
const scrollHandler = () => {
const newScroll = window.scrollY;
setCurrentScroll(newScroll);
if (prevScroll > newScroll) {
setIsActive(false);
} else {
setIsActive(true);
}
prevScroll = newScroll;
};
window.addEventListener("scroll", scrollHandler);
return () => window.removeEventListener("scroll", scrollHandler);
}, [currentScroll]);
return (
<header
className={`${
isActive ? "bg-white py-4 shadow-md" : "bg-none py-6"
} fixed w-full z-10 transition-all`}
>
<div className="container mx-auto flex items-center justify-between h-full">
<Link to={"/"}>
<div>
<img className="w-[40px]" src={Logo} alt="" />
</div>
</Link>
<div
onClick={() => setIsOpen(!isOpen)}
className="cursor-pointer flex relative"
>
<BsBag className="text-2xl" />
<div className="bg-red-500 absolute -right-2 -bottom-2 text-[12px] w-[18px] h-[18px] text-white rounded-full flex justify-center items-center">
{itemAmount}
</div>
</div>
</div>
</header>
);
};
export default Header;```