Skip to content

Latest commit

 

History

History
39 lines (26 loc) · 551 Bytes

ref.md

File metadata and controls

39 lines (26 loc) · 551 Bytes

React - ref

  • React에서 DOM요소를 다룰 때 사용한다.


useRef

  1. import하기
import { useRef } from 'react';

  1. 변수 생성 및 태그 명시
// useRef 함수를 할당 받는 변수 생성
const contentInput = useRef();

// ref property를 사용
<input ref={contentInput} value={content} type='text' />

  1. DOM 접근
const onSubmit = () => {

  // content의 길이가 5 미만이면 해당 input태그에 focus
  if(content.length < 5) {
    contentInput.current.focus();
  }
}