Navbar를 클릭하면 한 페이지 내에서 스크롤로 이동하는 페이지를 제작하고자 한다.

예제를 적용해보면 쉽게 이해가 가능하다. 어렵지 않은 메서드!
const handleScrollView = (
event: React.MouseEvent<HTMLDivElement, MouseEvent>
) => {
const name = (event.target as HTMLElement).innerText;
const menuTabs: { [key: string]: number } = {
컴포넌트1 : 0,
컴포넌트2 : 1,
컴포넌트3 : 2,
};
const targetElement = scrollRef.current[category[name]];
if (targetElement) {
targetElement.scrollIntoView({ behavior: "smooth" });
}
};
TS를 사용하고 있어서 event.target 에 대한 타입 설정이 쉽지 않았다.
<div>
<div ref={(el) => (scrollRef.current[0] = el)}>
<About />
</div>
<div ref={(el) => (scrollRef.current[1] = el)}>
<Archiving />
</div>
<div ref={(el) => (scrollRef.current[2] = el)}>
<Projects />
</div>
</div>