Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

new_bird-hyun

React - useRef로 DOM 제어하기 (스크롤 이동) 본문

코딩 공부

React - useRef로 DOM 제어하기 (스크롤 이동)

새혀니 2025. 4. 3. 20:58

import { useRef } from 'react';

export default function ScrollToSection() {
  const sectionRef = useRef(null);

  const scrollToSection = () => {
    sectionRef.current?.scrollIntoView({ behavior: 'smooth' });
  };

  return (
    <div>
      <button onClick={scrollToSection}>섹션으로 이동</button>
      <div style={{ height: '100vh' }} />
      <div ref={sectionRef} style={{ padding: 20, background: '#eee' }}>
        🎯 여기가 타겟 섹션입니다!
      </div>
    </div>
  );
}