[ React.js ] useRef로 스크롤 이동 기능 구현

SeungJin·2022년 8월 13일
0

React

목록 보기
14/19
import { useRef } from "react";

const Scroll = () => {
  const scrollBox = useRef();
  const scrollBtn = (text) => {
    const { scrollHeight, clientHeight } = scrollBox.current;
    if (text === "down") {
      scrollBox.current.scrollTop = scrollHeight - clientHeight;
    } else if (text === "up") {
      scrollBox.current.scrollTop = 0;
    }
  };
  return (
    <div>
      <div className={"scrollBody"} ref={scrollBox}>
        <div className={"scrollBox"} />
      </div>
      <button onClick={() => scrollBtn("down")}>down</button>
      <button onClick={() => scrollBtn("up")}>up</button>
    </div>
  );
};

export default Scroll;


profile
혼자 공부해 보고 적어두는 블로그입니다 문제 있다고 생각되시는 부분이 있으면 피드백이라도 남겨주시면 감사하겠습니다

0개의 댓글