useRef

ํ•™์Šต ํ‚ค์›Œ๋“œ

  • useRef

๐Ÿ“– useRef

  • ๋ Œ๋”๋ง์— ํ•„์š”ํ•˜์ง€ ์•Š๋Š” ๊ฐ’์„ ์ฐธ์กฐ ํ•  ์ˆ˜ ์žˆ๋Š” React hook

useRef๋Š” .current ํ”„๋กœํผํ‹ฐ๋กœ ์ „๋‹ฌ๋œ ์ธ์ž(initialValue)๋กœ ์ดˆ๊ธฐํ™”๋œ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ref ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜ํ™˜๋œ ๊ฐ์ฒด๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ „ ์ƒ์• ์ฃผ๊ธฐ๋ฅผ ํ†ตํ•ด ์œ ์ง€๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ƒํƒœ(state)๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์™€ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‹ค์‹œ ๋ Œ๋”๋งํ•˜์ง€๋งŒ, ๋ ˆํผ๋Ÿฐ์Šค ๊ฐ์ฒด์˜ ํ˜„์žฌ ๊ฐ’(current)์ด ๋ฐ”๋€Œ๋”๋ผ๋„ ๋ Œ๋”๋ง์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š”๋‹ค.

๐Ÿค” useRef ์–ธ์ œ ์‚ฌ์šฉํ•˜๋Š”๊ฐ€?

  • ์œ ์ง€๋˜๋Š” ๊ฐ’์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ(์ปดํฌ๋„ŒํŠธ ์•ˆ์˜ ๋ณ€์ˆ˜ ๋งŒ๋“ค๊ธฐ)(feat.๋ฆฌ๋ Œ๋”๋ง ๋ฐฉ์ง€)

    • ex. Input id ๊ด€๋ฆฌ (UI์— ๊ด€์—ฌํ•˜์ง€ ์•Š๋Š” ๊ฐ’)

    • ์‹ค๋ฌด : ๊ฒ€์ƒ‰ ํ›„ ์ด์ „์˜ ํ‚ค์›Œ๋“œ๋กœ ๊ฒ€์ƒ‰ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์œ ์ง€ ๋˜์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ

  • DOM ์š”์†Œ์— ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ

    • input์˜ ๊ฐ’์„ ์ž…๋ ฅํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ focus(์œ ํšจ์„ฑ์ฒดํฌ)

๐Ÿค– ํ™”๋ฉด์ด ๋ Œ๋”๋ง ๋˜์–ด๋„ ์œ ์ง€๋˜๋Š” ๊ฐ’์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ

๋ฆฌ์•กํŠธ์˜ state์™€ props์€ ๊ฐ’์ด ๋ณ€๊ฒฝ ๋  ๋•Œ ๋งˆ๋‹ค ๋ฆฌ๋ Œ๋”๋ง์ด ์ผ์–ด๋‚˜๊ฒŒ ๋œ๋‹ค. ๋งŒ์•ฝ ๊ฐ’์€ ๋ณ€๊ฒฝ๋˜์ง€๋งŒ ํ™”๋ฉด์˜ ๋ฆฌ๋ Œ๋”๋ง์ด ์ผ์–ด๋‚˜์ง€ ์•Š๊ฒŒ ํ•˜๋ ค๋ฉด? ๋˜ํ•œ ํ™”๋ฉด์ด ๋ฆฌ๋ Œ๋”๋ง ๋˜์–ด๋„ ๊ฐ’์ด ์œ ์ง€ ๋˜๋„๋ก ํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ

import { useRef, useState } from 'react';

export default function UseRef() {
  const [stateCount, setStateCount] = useState(0);
  const refCount = useRef(0);
  let count = 0;

  const handleStateUp = () => {
    setStateCount((stateCount) => stateCount + 1);
  };

  const handleLetUp = () => {
    count++;
    console.log(`๋ณ€์ˆ˜ count ํ™•์ธ ${count}`);
  };

  const handleRefUp = () => {
    refCount.current++;
    console.log(`ref count ํ™•์ธ ${refCount.current}`);
  };

  return (
    <div>
      <p> state : {stateCount}</p>
      <p> ๋ณ€์ˆ˜ : {count}</p>
      <p> ref : {refCount.current}</p>
      <button type="button" onClick={handleStateUp}>
        state up
      </button>
      <button type="button" onClick={handleLetUp}>
        ๋ณ€์ˆ˜ up
      </button>
      <button type="button" onClick={handleRefUp}>
        ref up
      </button>
    </div>
  );
}

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฒ˜๋Ÿผ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋˜์ง€ ์•Š๋‚˜?

์ปดํฌ๋„ŒํŠธ๋‚ด์—์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฒ˜๋Ÿผ let์„ ์ด์šฉํ•ด ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์„œ ๊ฐ’์„ ์ˆ˜์ •ํ•˜๋ฉด ํ™”๋ฉด์ด ๋ Œ๋”๋ง ๋˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋งŒ์•ฝ state ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์–ด ํ™”๋ฉด์ด ๋ฆฌ๋ Œ๋”๋ง ๋œ๋‹ค๋ฉด let์„ ์ด์šฉํ•ด ์„ ์–ธํ•œ ๋ณ€์ˆ˜๋Š” ๋‹ค์‹œ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ๋˜์–ด ๊ฐ’์ด ์œ ์ง€ ๋˜์ง€ ์•Š๋Š”๋‹ค.

๐Ÿค– DOM ์š”์†Œ์— ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ

JavaScript๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š”, ํŠน์ • DOM์„ ์„ ํƒํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์— getElementById, querySelector๊ฐ™์€ DOM Selector ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ DOM์„ ์„ ํƒํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋ฆฌ์•กํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด DOM์— ์ง์ ‘ ์ ‘๊ทผํ•  ์ผ์ด ์—†๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฆฌ์•กํŠธ๊ฐ€ renderํ•จ์ˆ˜๋ฅผ DOM์„ ์—…๋ฐ์ดํŠธ ํ•ด์ค€๋‹ค. ํ•˜์ง€๋งŒ ์•„๋ž˜์˜ ๊ฒฝ์šฐ์ฒ˜๋Ÿผ DOM ์š”์†Œ์— ์ ‘๊ทผ ํ•ด์•ผ ํ•˜๋Š” ๋ช‡๊ฐ€์ง€ ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค.

  • ์Šคํฌ๋กค ์œ„์น˜๋ฅผ ์ œ์–ดํ•ด์•ผ ํ•  ๋•Œ (ex.Top ๋ฒ„ํŠผ)

  • ํŠน์ • ์š”์†Œ์— ํฌ์ปค์Šค๋ฅผ ๋งž์ถฐ์•ผ ํ•  ๋•Œ

๐Ÿค– useRef ์‚ฌ์šฉ๋ฒ•

import { useRef} from 'react';

const ref = useRef(initialValue); 
import { useRef} from 'react';

export default function App(){

   const ref = useRef(null);
   console.log(ref);  // ์ถœ๋ ฅ {current: null}
}

useRef๋Š” ์ฒ˜์Œ์— ์ œ๊ณตํ•œ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์„ค์ •๋œ ๋‹จ์ผ current ํ”„๋กœํผํ‹ฐ๊ฐ€ ์žˆ๋Š” ref ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ref ๋‚ด๋ถ€์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด current์†์„ฑ์„ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค.

useRef ๋Œ€ํ‘œ์ ์ธ ์‚ฌ์šฉ ์˜ˆ์‹œ

import { useRef, useState } from 'react';

export default function UseRef() {

  const refCount = useRef(0);
  const inputEl = useRef(null);

  const handleRefUp = () => {
   refCount.current++;
   console.log(`ref count ํ™•์ธ ${refCount.current}`);
  };

  const handleFocus = () => {
    inputEl.current.focus();
  };

  return (
    <div>
      <p> ref : {refCount.current}</p>
      <button type="button" onClick={handleRefUp}>
        ref up
      </button>
      <div>
          <input 
          type="text" 
          placeholder="๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
          ref={inputEl}
          />
          <button type="button" onClick={handleFocus}>
            ๊ฒ€์ƒ‰
          </button>
      </div>
    </div>
  );
}

๐Ÿ”— ์ฐธ๊ณ 

Last updated