[React] React Hooks - useRef

Hannahhhยท2022๋…„ 8์›” 26์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
16/30

๐Ÿ” useRef


React์—์„œ focus, text selection, media playback, ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ ์šฉ, jd.js ๋ฐ greensock ๋“ฑ DOM ๊ธฐ๋ฐ˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™œ์šฉ๊ณผ ๊ฐ™์ด DOM ์—˜๋ฆฌ๋ฉ˜ํŠธ์˜ ์ฃผ์†Œ๊ฐ’์„ ํ™œ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•˜๋Š” hook ํ•จ์ˆ˜๋กœ, useRef ๋ฅผ ์‚ฌ์šฉํ•ด DOM ๋…ธ๋“œ, element, React ์ปดํฌ๋„ŒํŠธ ์ฃผ์†Œ๊ฐ’์„ ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ์ด ์ฃผ์†Œ๊ฐ’์€ ์ปดํฌ๋„ŒํŠธ๊ฐ€ re-render ๋˜๋”๋ผ๋„ ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค.

์œ„์— ๋ช…์‹œ๋œ ์˜ˆ์™ธ์ ์ธ ์ƒํ™ฉ์„ ์ œ์™ธํ•œ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ, useRef๋ฅผ ๋‚จ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋ถ€์ ์ ˆํ•˜๋‹ค.


  • ๋ฌธ๋ฒ•

// const refContainer = useRef(initialValue);
const ์ฃผ์†Œ๊ฐ’์„_๋‹ด๋Š”_๋ณ€์ˆ˜ = useRef(์ฐธ์กฐ์ž๋ฃŒํ˜•)
// ์ฃผ์†Œ๊ฐ’์„_๋‹ด๋Š”_๋ณ€์ˆ˜์— ์–ด๋–ค ์ฃผ์†Œ๊ฐ’์ด๋“  ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค.
return (
    <div>
      <input ref={์ฃผ์†Œ๊ฐ’์„_๋‹ด๋Š”_๋ณ€์ˆ˜} type="text" />
        {/* React์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ref๋ผ๋Š” ์†์„ฑ์— ์ฃผ์†Œ๊ฐ’์„_๋‹ด๋Š”_๋ณ€์ˆ˜๋ฅผ ๊ฐ’์œผ๋กœ ํ• ๋‹นํ•˜๋ฉด*/}
        {/* ์ฃผ์†Œ๊ฐ’์„_๋‹ด๋Š”_๋ณ€์ˆ˜์—๋Š” input DOM ์—˜๋ฆฌ๋จผํŠธ์˜ ์ฃผ์†Œ๊ฐ€ ๋‹ด๊น€. */}
        {/* ์ดํ›„, ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์—์„œ input DOM ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. */}
    </div>);

  • ์˜ˆ์‹œ
function TextInputWithFocusButton() {
  const inputEl = useRef(null);
  const onButtonClick = () => {
    // `current` points to the mounted text input element
    inputEl.current.focus();
  };
  return (
    <>
      <input ref={inputEl} type="text" />
      <button onClick={onButtonClick}>Focus the input</button>
    </>
  );
}




๐Ÿ‘€ ์‹ค์‚ฌ์šฉ ์˜ˆ์‹œ


โœ” focus


import React, { useRef } from "react";

const Focus = () => {
  const firstRef = useRef(null);
  const secondRef = useRef(null);
  const thirdRef = useRef(null);

  const handleInput = (event) => {
    console.log(event.key, event);
    if (event.key === "Enter") {
      if (event.target === firstRef.current) {
        secondRef.current.focus();
        event.target.value = "";
      } else if (event.target === secondRef.current) {
        thirdRef.current.focus();
        event.target.value = "";
      } else if (event.target === thirdRef.current) {
        firstRef.current.focus();
        event.target.value = "";
      } else {
        return;
      }
    }
  };

  return (
    <div>
      <h1>focus ์˜ˆ์ œ</h1>
      <h3>๊ฐ ๋นˆ์นธ์— ๋‹จ์–ด ์ž…๋ ฅ ํ›„ enter ๋ˆ„๋ฅผ ์‹œ, focus๊ฐ€ ๋‹ค์Œ ๋นˆ์นธ์œผ๋กœ ์ด๋™ํ•œ๋‹ค.</h3>
      <div>
        <label>์ฒซ ๋ฒˆ์งธ ๋นˆ์นธ์— text ์ž…๋ ฅ ํ›„, enter ๋ˆ„๋ฅผ ์‹œ, ๋‘ ๋ฒˆ์งธ ๋นˆ์นธ์œผ๋กœ focus ์ด๋™</label>
        <input ref={firstRef} onKeyUp={handleInput} />
      </div>
      <div>
        <label>๋‘ ๋ฒˆ์งธ ๋นˆ์นธ์— text ์ž…๋ ฅ ํ›„, enter ๋ˆ„๋ฅผ ์‹œ, ์„ธ ๋ฒˆ์งธ ๋นˆ์นธ์œผ๋กœ focus ์ด๋™</label>
        <input ref={secondRef} onKeyUp={handleInput} />
      </div>
      <div>
        <label>์„ธ ๋ฒˆ์งธ ๋นˆ์นธ์— text ์ž…๋ ฅ ํ›„, enter ๋ˆ„๋ฅผ ์‹œ, ์ฒซ ๋ฒˆ์งธ ๋นˆ์นธ์œผ๋กœ focus ์ด๋™</label>
        <input ref={thirdRef} onKeyUp={handleInput} />
      </div>
    </div>
  );
};

export default Focus;



โœ” media playback


import { useRef } from "react";

export default function App() {
  const videoRef = useRef(null);

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

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

  return (
    <div className="App">
      <div>
        <button onClick={playVideo}>Play</button>
        <button onClick={pauseVideo}>Pause</button>
		<button onClick={RemoveVideo}>Remove</button>
      </div>
      <video ref={videoRef} width="320" height="240" controls>
        <source
          type="video/mp4"
          src="https://player.vimeo.com/external/544643152.sd.mp4?s=7dbf132a4774254dde51f4f9baabbd92f6941282&profile_id=165"
        />
      </video>
    </div>
  );
}




Reference: ์ฝ”๋“œ์Šคํ…Œ์ด์ธ 

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