diff --git a/src/pages/main/components/floating/Floating.tsx b/src/pages/main/components/floating/Floating.tsx index 79c49982..c2480a62 100644 --- a/src/pages/main/components/floating/Floating.tsx +++ b/src/pages/main/components/floating/Floating.tsx @@ -1,6 +1,6 @@ import { useLogin, useModal } from "@hooks"; import { requestKakaoLogin } from "@utils/kakaoLogin"; -import { useEffect, useState } from "react"; +import { useEffect, useRef, useState } from "react"; import { useNavigate } from "react-router-dom"; import * as S from "./Floating.styled"; @@ -25,26 +25,19 @@ const Floating = () => { const [width, setWidth] = useState(window.innerWidth); const [showText, setShowText] = useState(true); + const lastScrollY = useRef(0); - useEffect(() => { - let lastScrollY = window.scrollY; - - const handleResize = () => { - setWidth(window.innerWidth); - }; - - const handleScroll = () => { - const currentScrollY = window.scrollY; - - if (currentScrollY > lastScrollY) { - setShowText(false); - } else if (currentScrollY < lastScrollY) { - setShowText(true); - } + const handleResize = () => { + setWidth(window.innerWidth); + }; - lastScrollY = currentScrollY; - }; + const handleScroll = () => { + const currentScrollY = window.scrollY; + setShowText(currentScrollY < lastScrollY.current); + lastScrollY.current = currentScrollY; + }; + useEffect(() => { window.addEventListener("resize", handleResize); window.addEventListener("scroll", handleScroll); return () => {