๐๋ฌธ์ 1. Navbar list๋ฅผ ํด๋ฆญํ๋ฉด ํด๋นํ๋ section์ผ๋ก scroll์ด๋ ์ด๋ฒคํธ๋ฅผ ์ฃผ๊ณ ์ถ์๋ค. window.scroll() ์ด๋ผ๋ webAPI๋ฅผ ์ด์ฉํ์๊ณ , ๊ฐ section์ offsetY ๊ฐ์ ๋ฐ์์ top ๊ฐ์ ๋์ ํ๋ค. ๊ทธ๋ฌ๋ ๋ชจ๋ํฐ ํ๋ฉด ํฌ๊ธฐ์ ๋ฐ๋ผ ์์น๊ฐ ๋ณํํ ๊ฒ์ด๊ณ , ์ ์ฐํ ์ฝ๋๊ฐ ์๋๋ผ ๊ฐ์ ์ ํ์์ฑ์ด ์์๋ค.
๐ซํด๊ฒฐ
๊ฐ section components ๋ง๋ค useRef๋ฅผ ๋ง๋ค์ด์ฃผ๊ณ , ๊ทธ๊ฒ๋ค์ offsetTop ๊ฐ์ window.scrollTop({top: offset...}) ๊ฐ์ ๋ฃ์ด์ค๋ค. ๊ทธ๋ฌ๋ ์ฌ๊ธฐ์ ๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
๐๋ฌธ์ 2.

Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
๐ซํด๊ฒฐ
โ ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ์์ ์ปดํฌ๋ํธ์ DOM์์์ ์ ๊ทผํ ๋๋ forwardRef๋ฅผ ์ฌ์ฉํ์ฌ ์ ๊ทผํด์ผ ํ๋ค.
๐ฉ
const aboutSection = useRef(null);
const projectSection = useRef(null);
const skillSection = useRef(null);
const contactSection = useRef(null);
const scrollToSection = (elementRef) => {
if (elementRef.current !== null) {
const offset = elementRef.current.offsetTop;
window.scrollTo({
top: offset - 100,
behavior: "smooth",
});
}
};
return (
<div>
<Navbar
scrollToSection={scrollToSection}
aboutSection={aboutSection}
projectSection={projectSection}
skillSection={skillSection}
contactSection={contactSection}
/>
section component ๋ง๋ค ref ์์ฑ์ ๋ฃ์ด์ฃผ๊ณ , ๊ทธ ํด๋น DOM์ offsetTop์ offset ๋ณ์์ ํ ๋นํ๋ค. ๊ทธ๋ฆฌ๊ณ ๊ฐ๊ฐ์ component๋ก ๊ฐ์ forwardRef๋ก ๋ถ๋ชจ ์ปดํฌ๋ํธ๊ฐ ๋ณด๋ด์ค ref๋ฅผ ๋ฐ๋๋ค.
๐ฉ