[Hook] useRef

๊ฐ•์€๋น„ยท2022๋…„ 1์›” 12์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
14/36

react ์Šคํ„ฐ๋””์—์„œ ๋ฆฌ์•กํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ธฐ์ˆ ์ด๋ผ๋Š” ์ฑ…์„ ์„ ์ •ํ–ˆ๊ณ  ์ด ์ฑ…์„ ์ฝ๊ณ  ๋ฐฐ์šด ๊ฒƒ์„ ๋ฐ”ํƒ•์œผ๋กœ ์ž‘์„ฑ๋˜์—ˆ๋‹ค.


๐Ÿ“Œ useRef

โœจ ํŠน์ • DOM ์„ ํƒ

  • ๋ฆฌ์•กํŠธ์—์„œ ํŠน์ • DOM์„ ์„ ํƒํ•  ๋•Œ useRef๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • DOM์— ์ง์ ‘์ ์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ
    • ํŠน์ • input์— ํฌ์ปค์Šค ์ฃผ๊ธฐ
    • ์Šคํฌ๋กค ๋ฐ•์Šค ์กฐ์ž‘ํ•˜๊ธฐ
    • canvas ์š”์†Œ์— ๊ทธ๋ฆผ ๊ทธ๋ฆฌ๊ธฐ ๋“ฑ
  • useRef() ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Ref ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ , ์ด ๊ฐ์ฒด๋ฅผ ์šฐ๋ฆฌ๊ฐ€ ์„ ํƒํ•˜๊ณ  ์‹ถ์€ DOM์˜ ref ๊ฐ’์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.
  • Ref ๊ฐ์ฒด์˜ .current ๊ฐ’์€ ์•ž์„œ ์„ ํƒํ•œ DOM ์„ ๊ฐ€๋ฅดํ‚ค๊ฒŒ ๋œ๋‹ค.

์˜ˆ์‹œ

import React, { useState, useRef } from 'react';

function InputSample() {
  const [inputs, setInputs] = useState({
    name: '',
    nickname: ''
  });
  
  const nameInput = useRef();

  const { name, nickname } = inputs;

  const onChange = e => {
    const { value, name } = e.target;
    setInputs({
      ...inputs,
      [name]: value
    });
  };

  const onReset = () => {
    setInputs({
      name: '',
      nickname: ''
    });
    nameInput.current.focus();
  };

  return (
    <div>
      <input
        name="name"
        placeholder="์ด๋ฆ„"
        onChange={onChange}
        value={name}
        ref={nameInput}
      />
      <input
        name="nickname"
        placeholder="๋‹‰๋„ค์ž„"
        onChange={onChange}
        value={nickname}
      />
      <button onClick={onReset}>์ดˆ๊ธฐํ™”</button>
      <div>
        <b>๊ฐ’: </b>
        {name} ({nickname})
      </div>
    </div>
  );
}

export default InputSample;

โœจ ์ปดํฌ๋„ŒํŠธ ์•ˆ์˜ ๋ณ€์ˆ˜

  • useRef๋ฅผ ์ด์šฉํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ ์กฐํšŒ ๋ฐ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ด๋•Œ useRef๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๋ณ€์ˆ˜๊ฐ€ ๋ฐ”๋€๋‹ค๊ณ  ํ•ด์„œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง๋˜์ง€ ์•Š๋Š”๋‹ค.
  • useRef ๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜๋Š” ์„ค์ • ํ›„ ๋ฐ”๋กœ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ’ ๊ด€๋ฆฌ
    • setTimeout, setInterval ์„ ํ†ตํ•ด์„œ ๋งŒ๋“ค์–ด์ง„ id
    • ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค
    • scroll ์œ„์น˜

๐Ÿ’ก ์ผ๋ฐ˜ ๋กœ์ปฌ ๋ณ€์ˆ˜ ๋Œ€์‹  useRef๋ฅผ ์ด์šฉํ•˜๋Š” ์ด์œ ?

  • ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋„ ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— re-rendering ๋˜๋ฉด ๋กœ์ปฌ ๋ณ€์ˆ˜๋“ค์„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
  • ๋ฐ˜๋ฉด useRef๋กœ ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด๋Š” React๊ฐ€ ๋งŒ๋“  ์ „์—ญ ์ €์žฅ์†Œ์— ์ €์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌ๋ Œ๋”๋ง๋˜๋”๋ผ๋„ ๋งˆ์ง€๋ง‰์œผ๋กœ ์—…๋ฐ์ดํŠธํ•œ current ๊ฐ’์ด ์œ ์ง€๋œ๋‹ค.


โœจ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์˜ ref

์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ํ†ตํ•œ ref ์„ค์ •

<input ref = { (ref) => {this.input = ref}} />

createRef๋ฅผ ํ†ตํ•œ ref ์„ค์ •

import { Component } from "react";

class RefSample extends Component {
    input = React.createRef();
    
    handleFocus = () => {
        this.input.current.focus();
    }
    
    render() {
        return (
            <div>
               <input ref = {this.input}/>
            </div>
        );
    }
}

0๊ฐœ์˜ ๋Œ“๊ธ€