Open choigirang opened 8 months ago
// Header.tsx
...
useEffect(() => {
const scrollChangeRouter = () => {
const section = lists.find(list => {
const element = document.getElementById(list);
if (element) {
const rect = element.getBoundingClientRect();
return rect.top <= window.innerHeight / 2 && rect.bottom >= window.innerHeight / 2;
}
return false;
});
if (section && section !== activeSection) {
setActiveSection(section);
window.history.replaceState(null, '', `/${section}`);
}
};
window.addEventListener('scroll', scrollChangeRouter);
return () => {
window.removeEventListener('scroll', scrollChangeRouter);
};
}, [activeSection]);
const navigate = useNavigate()
의 navigate(section)
으로 해결
의존성 배열 자체를 잘못 작성한 경우의 수?
const moveTurtle = () => { if (theme === 'dark') return; if (turtleX <= window.innerWidth) { setTurtleX(prev => prev + 10); } else { setTurtleX(0); } };
useEffect(() => { console.log(currentPath, checkUrl, 'in checkUrl'); if (currentPath === '/Home' || currentPath === '/') setCheckUrl(true); else setCheckUrl(false); }, [currentPath, checkUrl]);
useEffect(() => { console.log(currentPath, checkUrl, 'in useEffect');
if ((theme !== 'dark' && currentPath === '/Home') || currentPath === '/') { const intervalX = setTimeout(() => { moveTurtle(); console.log(currentPath, checkUrl, 'in setTime'); }, 5000);
return () => clearInterval(intervalX); } }, [turtleX, theme, currentPath]);
return ( <Container $turtleX={turtleX}>
); }