๐ŸชuseRef()

์ง€์€ยท2022๋…„ 11์›” 16์ผ
0

โš›๏ธ React

๋ชฉ๋ก ๋ณด๊ธฐ
11/23
post-custom-banner

useRef()

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

  • ๋ฐ˜ํ™˜๋œ ref ๊ฐ์ฒด๋Š” State(์ƒํƒœ)์ฒ˜๋Ÿผ ์ปดํฌ๋„ŒํŠธ์˜ ์ „ Lifecycle์— ๊ฑธ์ณ ์œ ์ง€๋œ๋‹ค.
    ( = ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง๋˜์–ด๋„ ๊ฐ’์ด ์œ ์ง€๋œ๋‹ค.)
  • useRef() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ „๋‹ฌ ์ธ์ž๋กœ ์ดˆ๊ธฐ๊ฐ’์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.
import { useRef } from "react";

const ref = useRef(์ดˆ๊ธฐ๊ฐ’);
console.log(ref); // { current: ์ดˆ๊ธฐ๊ฐ’ }
  • ์ดˆ๊ธฐ๊ฐ’์„ ์ „๋‹ฌํ•˜์ง€ ์•Š์œผ๋ฉด undefined๊ฐ€ ํ• ๋‹น๋œ๋‹ค.
const ref = useRef();
console.log(ref); // { current: undefined }

useRef()๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

1. ๋ณ€์ˆ˜ ๊ด€๋ฆฌ

์ด ref ๊ฐ์ฒด์˜ current ์†์„ฑ์— ๊ฐ’์„ ๋‹ด์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์›ํ•˜๋Š” ๋Œ€๋กœ ๊ฐ’์„ ๋ณ€๊ฒฝ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
์ค‘์š”ํ•œ ์ ์€, State์™€ ๋‹ค๋ฅด๊ฒŒ ref ๊ฐ์ฒด์˜ current ์†์„ฑ๊ฐ’์€ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์–ด๋„ ์ปดํฌ๋„ŒํŠธ ๋ฆฌ๋ Œ๋”๋ง์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

ref.current = 1;  // ์ปดํฌ๋„ŒํŠธ ๋ฆฌ๋ Œ๋”๋ง X
console.log(ref); // { current: 1 } 

๋”ฐ๋ผ์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ–ˆ์„ ๋•Œ ๋ฆฌ๋ Œ๋”๋ง์„ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š์•„์•ผ ํ•˜๋Š” ๊ฐ’์„ ๋‹ค๋ฃฐ ๋•Œ useRef๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


2. DOM ์กฐ์ž‘

ref ๊ฐ์ฒด์—๋Š” ๋ณ€์ˆ˜ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, DOM ๋…ธ๋“œ, ์—˜๋ฆฌ๋จผํŠธ, React ์ปดํฌ๋„ŒํŠธ์˜ ์ฃผ์†Œ๊ฐ’์„ ์ฐธ์กฐ(reference)ํ•˜๊ฒŒ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

const ref = useRef(null);
console.log(ref);         // {current: HTMLInputElement}
console.log(ref.current); // <input type="text"></input>

return (
  <div>
    <input ref={ref} type="text" />
  </div>
);

โžก๏ธ ref ๊ฐ์ฒด์— input ์š”์†Œ๊ฐ€ ๋‹ด๊ธด ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋ ‡๊ฒŒ input ์š”์†Œ๋ฅผ ๋‹ด๊ณ ์žˆ๋Š” ref.current์— focus()์™€ ๊ฐ™์€ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด input ์š”์†Œ์— ํฌ์ปค์Šค๊ฐ€ ์ด๋™ํ•˜๊ฒŒ ํ•œ๋‹ค.

์˜ˆ์‹œ 1) focus

const ref = useRef(null);

const focusToInput = () => {
  ref.current.focus(); // = input.focus()
}

return (
    <div>
      <input ref={ref} type="text"/>
      <button onClick={focusToInput}>Click Me</button>
   	</div>

์ด ๋ฐ–์—๋„ input.value()๋กœ input์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์›ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ DOM ์š”์†Œ๋ฅผ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

โš ๏ธ ํ•˜์ง€๋งŒ ๊ธฐ๋ณธ React ๋ฌธ๋ฒ•์„ ๋ฒ—์–ด๋‚˜ useRef๋ฅผ ๋‚จ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋ถ€์ ์ ˆํ•˜๊ณ , React์˜ ํŠน์ง•์ด์ž ์žฅ์ ์ธ ์„ ์–ธํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์›์น™๊ณผ ๋ฐ˜๋Œ€๋˜๊ธฐ ๋•Œ๋ฌธ์— ์กฐ์‹ฌํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

์˜ˆ์‹œ 2) media playback

๋น„๋””์˜ค ์š”์†Œ๋ฅผ ref ๊ฐ์ฒด์— ๋‹ด์•„ play(), pause(), remove() ๋“ฑ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

const videoRef = useRef(null);
console.log(videoRef.current); // <video width="320" height="240" controls="">โ€ฆ</video>

  const playVideo = () => {
    videoRef.current.play();
  };

  const pauseVideo = () => {
    videoRef.current.pause();
  };

  const removeVideo = () => {
    videoRef.current.remove();
  };

  return (
    <div className="App">
      <div>
        <button onClick={playVideo}>์žฌ์ƒ</button>
        <button onClick={pauseVideo}>์ผ์‹œ์ •์ง€</button>
        <button onClick={removeVideo}>์‚ญ์ œ</button>
      </div>
      <video ref={videoRef} width="320" height="240" controls>
        <source
          type="video/mp4"
          src="..."
        />
      </video>
    </div>
profile
๋ธ”๋กœ๊ทธ ์ด์ „ -> https://janechun.tistory.com
post-custom-banner

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