I want to know if the user has scrolled or not to update the UI in NextJS. I have the following code, all the examples I have found have the same code:
const [scrollY, setScrollY] = useState(0);
const onScroll = (event) => {
const { pageYOffset, scrollY } = window;
console.log("yOffset", pageYOffset, "scrollY", scrollY);
setScrollY(window.pageYOffset);
};
useEffect(() => {
document.body.addEventListener("scroll", onScroll, { passive: true });
// remove event on unmount to prevent a memory leak
() => document.removeEventListener("scroll", onScroll, { passive: true });
}, []);
But the scroll does not get updated, neither with document nor window. I always get the same output:
Any suggestion? Thanks:)
