Front-end/React

[React] useRef (focus, 값 자동증가)

Bay Im 2024. 3. 31. 13:29
  • 현재 input 창 포커스로 두껍게 하기
    • useRef() 사용
      • const refName = useRef(null); 변수 선언 후
      • refName.current.focus(); 사용하면 해당 input 창이 두꺼워 지면서 포커스된다.
  • id 값 자동 증가시키기
    • const last = useRef(3);
    • useState 에서 id: last.current 로 주기
    • 메서드에선 last.current++; 로 자동 증감 시키기
  • 정수 초기값 설정 후 상태 변경
    • useRef 사용
      • const newCode = useRef(103);
    • 값은 current를 사용하여 가져와야 한다.
      • const [product, setProduct] = useState({
            code: newCode.current,
    • 값 증가는 ++ 사용
      • code: ++newCode.current,
728x90