-
[react-scroll] navbar 클릭 시, 해당 컴포넌트로 스크롤을 이동하려면?React.js/react 2023. 7. 23. 01:00
1️⃣ react-scroll 라이브러리를 설치한다.
$ npm install react-scroll2️⃣ navbar에 <Link>를 다는데, 중요한 건 Link를 "react-router-dom"이 아닌 "react-scroll"에서 import 한다.
import { Link } from "react-scroll"; <ul> <li> <Link activeClass="active" to="introSection" spy={true} smooth={true} duration={400} > INTRO </Link> </li> ... </ul>https://www.npmjs.com/package/react-scroll
react-scroll
A scroll component for React.js. Latest version: 1.8.9, last published: 7 months ago. Start using react-scroll in your project by running `npm i react-scroll`. There are 654 other projects in the npm registry using react-scroll.
www.npmjs.com
옵션값을 확인해 볼 수 있다.
3️⃣ Link의 to 옵션값을 이동하길 원하는 요소의 id로 할당해준다.
<div id="introSection"> ... </div>4️⃣ navbar를 클릭하면 해당 요소로 잘 이동하는 것을 확인할 수 있다. 끗!
'React.js > react' 카테고리의 다른 글
[react] react-router-dom으로 페이지 접근을 막자! (0) 2023.08.23 [react] 카카오 지도(kakao map) 띄우기 (0) 2023.08.18 [React] React Color (0) 2023.07.05 [React] dom-to-image로 DOM 이미지 다운받기 (0) 2023.07.05 [React] 초간단 모달창 띄우기 (0) 2023.06.27